栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

样式设置器中的UWP绑定不起作用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

样式设置器中的UWP绑定不起作用

如MSDN 上Setter.Value属性页上的“ 迁移说明 ”部分所述,UWP /
Windows运行时不支持样式设置器中的绑定。

Windows Presentation Foundation(WPF)和Microsoft
Silverlight支持使用Binding表达式为样式中的Setter提供值的功能。Windows运行时不支持Setter.Value的绑定用法(该绑定不会评估,该Setter无效,您不会收到错误,但也不会获得所需的结果)。从WPF或Silverlight
XAML转换XAML样式时,请使用设置值的字符串或对象替换任何绑定表达式用法,或将这些值重构为共享的{StaticResource}标记扩展值,而不是绑定获得的值。

解决方法可能是为绑定的源路径附加属性的帮助程序类。它在helper属性的PropertyChangedCallback中的代码后面创建绑定:

public class BindingHelper{    public static readonly DependencyProperty GridColumnBindingPathProperty =        DependencyProperty.RegisterAttached( "GridColumnBindingPath", typeof(string), typeof(BindingHelper), new Propertymetadata(null, GridBindingPathPropertyChanged));    public static readonly DependencyProperty GridRowBindingPathProperty =        DependencyProperty.RegisterAttached( "GridRowBindingPath", typeof(string), typeof(BindingHelper), new Propertymetadata(null, GridBindingPathPropertyChanged));    public static string GetGridColumnBindingPath(DependencyObject obj)    {        return (string)obj.GetValue(GridColumnBindingPathProperty);    }    public static void SetGridColumnBindingPath(DependencyObject obj, string value)    {        obj.SetValue(GridColumnBindingPathProperty, value);    }    public static string GetGridRowBindingPath(DependencyObject obj)    {        return (string)obj.GetValue(GridRowBindingPathProperty);    }    public static void SetGridRowBindingPath(DependencyObject obj, string value)    {        obj.SetValue(GridRowBindingPathProperty, value);    }    private static void GridBindingPathPropertyChanged(        DependencyObject obj, DependencyPropertyChangedEventArgs e)    {        var propertyPath = e.NewValue as string;        if (propertyPath != null)        { var gridProperty =     e.Property == GridColumnBindingPathProperty     ? Grid.ColumnProperty     : Grid.RowProperty; BindingOperations.SetBinding(     obj,     gridProperty,     new Binding { Path = new PropertyPath(propertyPath) });        }    }}

您可以像这样在XAML中使用它们:

<ItemsControl.ItemContainerStyle>    <Style TargetType="ContentPresenter">        <Setter Property="local:BindingHelper.GridColumnBindingPath" Value="Level"/>        <Setter Property="local:BindingHelper.GridRowBindingPath" Value="Row"/>    </Style></ItemsControl.ItemContainerStyle>

有关绝对定位(即,绑定

Canvas.Left
canvas.Top
属性)的简单解决方法,请参见此答案。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/568478.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号