样式设置器中的 UWP 绑定不起作用 [英] UWP Binding in Style Setter not working

查看:19
本文介绍了样式设置器中的 UWP 绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建 xaml 控件时遇到问题.我正在通用应用程序中的 VS 2015 中编写新项目.我想创建网格.在这个网格中,我想要一个按钮.在模型中,我指定了列(级别)和行.这是我的代码:

<ItemsControl.ItemsPanel><ItemsPanelTemplate><网格><Grid.RowDefinitions><RowDefinition Height="10*"/><RowDefinition Height="10*"/><RowDefinition Height="10*"/><RowDefinition Height="10*"/><RowDefinition Height="10*"/><RowDefinition Height="10*"/><RowDefinition Height="10*"/><RowDefinition Height="10*"/><RowDefinition Height="10*"/><RowDefinition Height="10*"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="14*"/><ColumnDefinition Width="14*"/><ColumnDefinition Width="14*"/><ColumnDefinition Width="14*"/><ColumnDefinition Width="14*"/><ColumnDefinition Width="14*"/><ColumnDefinition Width="14*"/></Grid.ColumnDefinitions></网格></ItemsPanelTemplate></ItemsControl.ItemsPanel><ItemsControl.ItemContainerStyle><Style TargetType="控件"><Setter Property="Grid.Column" Value="{绑定级别}"/><Setter Property="Grid.Row" Value="{绑定行}"/></风格></ItemsControl.ItemContainerStyle><ItemsControl.ItemTemplate><数据模板><按钮内容="{绑定名称}"/></数据模板></ItemsControl.ItemTemplate></ItemsControl>

我在行 <Setter Property="Grid.Column" Value="{Binding Level}"/> 中遇到错误错误:来自 HRESULT 的异常:0x8000FFFF (E_UNEXPECTED) 在 edytor 中而不是在运行代码中.怎么了?在旧"WPF 中一切正常,但在适用于 Windows 10 的通用应用程序中出现错误.谁能帮我 ?

解决方案

Setter.Value 属性 MSDN 上的页面,UWP/Windows 运行时不支持样式设置器中的绑定.><块引用>

Windows Presentation Foundation (WPF) 和 Microsoft Silverlight支持使用绑定表达式来提供值的能力对于风格中的二传手.Windows 运行时不支持绑定Setter.Value 的用法(绑定不会评估并且 Setter 有没有效果,你不会得到错误,但你不会得到想要的结果任何一个).当您从 WPF 或 Silverlight XAML 转换 XAML 样式时,用设置的字符串或对象替换任何绑定表达式用法值,或将值重构为共享的 {StaticResource} 标记扩展值而不是绑定获得的值.

解决方法可能是使用附加属性的辅助类,用于绑定的源路径.它在辅助属性的 PropertyChangedCallback 后面的代码中创建绑定:

公共类BindingHelper{公共静态只读 DependencyProperty GridColumnBindingPathProperty =DependencyProperty.RegisterAttached("GridColumnBindingPath", typeof(string), typeof(BindingHelper),新的 PropertyMetadata(null, GridBindingPathPropertyChanged));公共静态只读 DependencyProperty GridRowBindingPathProperty =DependencyProperty.RegisterAttached("GridRowBindingPath", typeof(string), typeof(BindingHelper),新的 PropertyMetadata(null, GridBindingPathPropertyChanged));公共静态字符串 GetGridColumnBindingPath(DependencyObject obj){返回(字符串)obj.GetValue(GridColumnBindingPathProperty);}公共静态无效 SetGridColumnBindingPath(DependencyObject obj,字符串值){obj.SetValue(GridColumnBindingPathProperty, value);}公共静态字符串 GetGridRowBindingPath(DependencyObject obj){返回(字符串)obj.GetValue(GridRowBindingPathProperty);}public static void SetGridRowBindingPath(DependencyObject obj,字符串值){obj.SetValue(GridRowBindingPathProperty, value);}私有静态无效GridBindingPathPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e){var propertyPath = e.NewValue as string;如果(属性路径!= null){var gridProperty =e.Property == GridColumnBindingPathProperty?Grid.Column 属性: Grid.RowProperty;BindingOperations.SetBinding(目标,网格属性,新绑定{ Path = new PropertyPath(propertyPath) });}}}

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

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

<小时>

有关绝对定位的简单解决方法(即绑定Canvas.Leftcanvas.Top 属性),请参阅这个答案.

I have problem with creating xaml control. I'm writing new project in VS 2015 in universal app. I want create grid. In this grid I want to have a button. In model I specifi the column (Level) and Row. this is my code:

<ItemsControl Grid.Row="1" ItemsSource="{Binding Path=TechnologyList}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="14*"/>
                    <ColumnDefinition Width="14*"/>
                    <ColumnDefinition Width="14*"/>
                    <ColumnDefinition Width="14*"/>
                    <ColumnDefinition Width="14*"/>
                    <ColumnDefinition Width="14*"/>
                    <ColumnDefinition Width="14*"/>
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="Control">
            <Setter Property="Grid.Column" Value="{Binding Level}" />
            <Setter Property="Grid.Row" Value="{Binding Row}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding Name}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

I get a error in line <Setter Property="Grid.Column" Value="{Binding Level}" /> The error: Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED) was in edytor not in running code. What is wrong? In "old" WPF everything was OK but in Universal App for Windows 10 I have a error. Can anyone help me ?

解决方案

As noted in the section Migration notes on the Setter.Value property page on MSDN, UWP/Windows Runtime does not support bindings in Style Setters.

Windows Presentation Foundation (WPF) and Microsoft Silverlight supported the ability to use a Binding expression to supply the Value for a Setter in a Style. The Windows Runtime doesn't support a Binding usage for Setter.Value (the Binding won't evaluate and the Setter has no effect, you won't get errors, but you won't get the desired result either). When you convert XAML styles from WPF or Silverlight XAML, replace any Binding expression usages with strings or objects that set values, or refactor the values as shared {StaticResource} markup extension values rather than Binding-obtained values.

A workaround could be a helper class with attached properties for the source paths of the bindings. It creates the bindings in code behind in a PropertyChangedCallback of the helper property:

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) });
        }
    }
}

You would use them in XAML like this:

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


For a simple workaround for absolute positioning (i.e. binding the Canvas.Left and canvas.Top properties), see this answer.

这篇关于样式设置器中的 UWP 绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆