在 WinUI 3 中的样式设置器中进行绑定

问题描述 投票:0回答:2
binding styles setter winui-3
2个回答
0
投票

显然,WinUI 3 尚不支持 Setter 中的常规绑定,尽管这是一项备受期待的功能。解决方法是创建一个包含 DependencyProperty 的帮助程序类,每当更改/设置属性时,该类都会调用更改处理程序。然后,更改处理程序可以在代码中创建所需的绑定。感谢 clemens,他很久以前就为 UWP 提出了类似的建议。这是一个辅助类示例:

internal class BindingHelper
{
    #region CompactPaneLengthBindingPath
    public static readonly DependencyProperty CompactPaneLengthBindingPathProperty = DependencyProperty.RegisterAttached(
            "CompactPaneLengthBindingPath", typeof(string), typeof(BindingHelper), new PropertyMetadata(null, BindingPathChanged));
            
    public static string GetCompactPaneLengthBindingPath(DependencyObject obj)
    {
        return (string)obj.GetValue(CompactPaneLengthBindingPathProperty);
    }
    
    public static void SetCompactPaneLengthBindingPath(DependencyObject obj, string value)
    {
        obj.SetValue(CompactPaneLengthBindingPathProperty, value);
    }
    #endregion
        
    #region HeightBindingPath
    // another DP is defined here (all of them are strings)
        
    #region ForegroundBindingPath
    // and a third one, etc.
        

    // ===================== Change Handler: Creates the actual binding

    private static void BindingPathChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue is string source)                                                                // source property (could add DataContext by setting Value="source@datacontext" for example)
        {
            DependencyProperty target;                                                                  // which property is the target of the binding?
            if (e.Property == CompactPaneLengthBindingPathProperty) target = NavigationView.CompactPaneLengthProperty;
            else if (e.Property == HeightBindingPathProperty) target = FrameworkElement.HeightProperty;
            else if (e.Property == ForegroundBindingPathProperty) target = Control.ForegroundProperty;
            else throw new System.Exception($"BindingHelper: Unknown target ({nameof(e.Property)}");    // don't know this property
        
            obj.ClearValue(target);                                                                     // clear previous bindings (and value)
            BindingOperations.SetBinding(obj, target,                                                   // set new binding (and value)
               new Binding { Path = new PropertyPath(source), Mode = BindingMode.OneWay });
        }
    }

请注意,所有 DependencyProperties 都是字符串类型,并且目标类型可以是您正在使用的控件的任何祖先类型。例如,

HeightBindingPathProperty
绑定可以与任何 FrameworkElement 一起使用。

在 Style 中使用 helper 就像使用任何 Setter 一样,如下所示:

<Style x:Key="MyNavigationView" TargetType="controls:NavigationView" >
    <Setter Property="local:BindingHelper.CompactPaneLengthBindingPath" Value="CurrentCompactPaneLength" />
</Style>

我希望这有帮助。


0
投票

这对我有用:

    <Page.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="FontSize" Value="{x:Bind ViewModel.FontSize, Mode=OneWay}" />
    </Style>
</Page.Resources>
© www.soinside.com 2019 - 2024. All rights reserved.