UWP装订风格安装程序无法正常工作

我有创buildxaml控制的问题。 我正在通用应用程序中编写VS 2015中的新项目。 我想创build网格。 在这个网格中,我想要一个button。 在模型中,我指定了列(Level)和行。 这是我的代码:

<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> 

我得到一个错误行<Setter Property="Grid.Column" Value="{Binding Level}" />错误:exception从HRESULT:0x8000FFFF(E_UNEXPECTED)在edytor不在运行的代码。 哪里不对? 在“老”WPF一切正常,但在通用应用程序的Windows 10我有一个错误。 任何人都可以帮我吗?

正如MSDN上Setter.Value属性页上的Migration注释部分所述,UWP / Windows运行时不支持在样式设置器中进行绑定。

Windows Presentation Foundation(WPF)和Microsoft Silverlight支持使用Bindingexpression式为样式中的Setter提供值的function。 Windows运行时不支持Setter.Value的Binding用法(Binding不会计算,Setter没有效果,你不会得到错误,但是你也不会得到期望的结果)。 当您从WPF或Silverlight XAML转换XAML样式时,请将任何绑定expression式用法replace为设置值的string或对象,或将值重构为共享{StaticResource}标记扩展值而不是绑定获取的值。

解决方法可能是为绑定的源path附加属性的助手类。 它在辅助属性的PropertyChangedCallback中创build了代码中的绑定:

 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.Leftcanvas.Top属性)的简单解决方法,请参阅此答案 。