English 中文(简体)
程序检索由设置器在一个样式内设置的依附属性Property
原标题:Programmatically retrieve DependencyProperty set by Setters within a Style
Working on a WinUI3 App. I have defined a style in my XMAL to use when creating controls programmatically later the resource is used Button button = new() { Content = node.Key, Style = this.visualizationCanvas.Resources["NodeStyle"] as Style, ClickMode = ClickMode.Release }; And it works. But, I can t access the properties values set by the setters within the style, within the code, in a controlled manner. I read the following documents https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.setter.target?view=windows-app-sdk-1.5 https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.dependencyproperty?view=windows-app-sdk-1.5 https://learn.microsoft.com/en-us/windows/uwp/xaml-platform/dependency-properties-overview I have learned that it should be possible as it states If you re accessing a Setter instance using code, you cannot change the value of any property of a Setter instance if the value of the IsSealed property on a parent Style is true. Tried: Style s = this.visualizationCanvas.Resources["NodeStyle"] as Style; _log.Info(s.TargetType.FullName); ================= output: Microsoft.UI.Xaml.Controls.Button Then: foreach (Setter setter in s.Setters) { setter.Property.GetMetadata(typeof(Button)).DefaultValue; //NaN setter.Value; // gives me the values, but I don t know which property the value belongs to setter.GetValue(Button.HeightProperty); // System.Runtime.InteropServices.COMException (0x8000FFFF) setter.GetValue(setter.Property); // System.Runtime.InteropServices.COMException (0x8000FFFF) }
问题回答
You can get the property values for each setter like this: if (Resources.TryGetValue("NodeStyle", out var resource) is not true || resource is not Style nodeStyle) { return; } var styledButton = new Button { Style = nodeStyle }; var properties = nodeStyle.TargetType .GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) .ToList(); foreach (Setter setter in nodeStyle.Setters.Cast()) { if (properties .Where(propertyInfo => (propertyInfo.GetValue(styledButton) as DependencyProperty) == setter.Property) .FirstOrDefault() is not { } targetProperty || styledButton.GetValue(setter.Property) is not { } value) { continue; } System.Diagnostics.Debug.WriteLine($"{targetProperty.Name} Value: {value}"); } Output HeightProperty Value: 90 WidthProperty Value: 180 CornerRadiusProperty Value: 100,100,100,100 BackgroundProperty Value: Microsoft.UI.Xaml.Media.SolidColorBrush




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签