C# items源集合上的WPF ItemsControl.ItemContainerStyle setter属性无法引用这些属性
编辑:看来棋子项目的绑定是一个重竖琴问题——我关闭了它,错误消失了。难以置信 我一直在尝试将集合中的项目绑定到网格位置。我已经能够绑定集合并绘制项,但在XAML中,我无法引用所需的属性 根据这个例子: 守则:C# items源集合上的WPF ItemsControl.ItemContainerStyle setter属性无法引用这些属性,c#,wpf,xaml,C#,Wpf,Xaml,编辑:看来棋子项目的绑定是一个重竖琴问题——我关闭了它,错误消失了。难以置信 我一直在尝试将集合中的项目绑定到网格位置。我已经能够绑定集合并绘制项,但在XAML中,我无法引用所需的属性 根据这个例子: 守则: public class ChessPiece { public string Text { get; set; } public int Row { get; set; } // 0..n-1 public int Column { get; set; } //
public class ChessPiece
{
public string Text { get; set; }
public int Row { get; set; } // 0..n-1
public int Column { get; set; } // 0..n-1
public ChessPiece(string text, int row, int col)
{
Text = text;
Row = row;
Column = col;
}
}
public class MainViewModel : ViewModelBase
{
public ObservableCollection<ChessPiece> ChessPieces { get; private set; }
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
ChessPieces = new ObservableCollection<ChessPiece>();
ChessPieces.Add(new ChessPiece("QR-BLK", 1, 1));
ChessPieces.Add(new ChessPiece("QN-BLK", 1, 2));
ChessPieces.Add(new ChessPiece("QB-BLK", 0, 3));
////if (IsInDesignMode)
////{
//// // Code runs in Blend --> create design time data.
////}
////else
////{
//// // Code runs "for real"
////}
}
}
XAML:
在mainviewmodel中,我有一个可观察的棋子集合,我用新的棋子填充它
我可以在XAML的其他级别绑定到Row属性,但不能在样式设置器中绑定,在样式设置器中,我试图将它绑定到网格位置—集合可以绑定,但不能绑定到单个项
任何帮助都将不胜感激 使用简单绑定几乎不可能做到这一点。您需要设置行/网格的是容器,而不是模板生成的控件,并且使用它并不有趣 我的解决方案是创建自己的特殊网格,并使用ArrangeOverride:
当然,获取正确的数据片段并非易事,但与从模板内部设置container Grid.Row属性相比,这要容易得多。请给我们一个简单的示例,而不是将缩写代码或片段转储到我们身上。它将帮助每个人更好地理解你的问题,保持你的问题整洁,并使你更有可能得到好的答案。猜一猜,把样式放在ItemsControl.Rsources中。@Giallo当我创建了64个元素的列表时,它对我很有效。这是否意味着在您的情况下,所有值在一个单元格中相互重叠,或者您什么也看不到?@dkozl我在DataContext中得到一个类型为“Giallo.MainViewModel”的属性列,这是我的页面视图模型。只是那些设置器,它们只能看到ChessPieces可观察集合,而不是ChessPiece.Row或ChessPiece.Column。真的不知道如何解决这个问题。@Giallo如果棋子是ObservableCollection,那么每个项目的DataContext都将是棋子类型,因此您的样式应该并且确实可以正常工作
<Window x:Class="MVVMLightTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding Main, Mode=OneWay, Source={StaticResource Locator}}" Height="550" Width="525">
<Grid Margin="0,0,0,0">
<ItemsControl ItemsSource="{Binding ChessPieces}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid ShowGridLines="True">
<Grid.RowDefinitions >
<RowDefinition Height="162.667"></RowDefinition>
<RowDefinition Height="171.333"></RowDefinition>
<RowDefinition Height="165.333"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="162"></ColumnDefinition>
<ColumnDefinition Width="206">/>
<ColumnDefinition Width="263"/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Text}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Row" Value="{Binding Row}"/>
<Setter Property="Grid.Column" Value="{Binding Column}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Grid>
</Window>
class DynamicGrid : Grid
{
protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize)
{
UIElementCollection elements = base.InternalChildren;
for (int i = 0; i < elements.Count; i++)
{
elements[i].SetValue(Grid.RowProperty, correctRow);
}
return base.ArrangeOverride(arrangeSize);
}
}
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<local:DynamicGrid />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>