我被困在以下系結問題上。我在 MainWindow.xaml.cs 中有以下類宣告:
public List<MyArrayClass> MyArrayData { get; set; }
public class MyArrayClass
{
public ObservableCollection<int> MyArrayInt { get; set; }
public ObservableCollection<Thickness> MyArrayBorder { get; set; }
}
我創建物件如下:
MyArrayClass sample3 = new MyArrayClass
{
MyArrayInt = new ObservableCollection<int> { 0, 1, 2, 3 },
MyArrayBorder = new ObservableCollection<Thickness> { new Thickness(0,0,0,2), new Thickness(0, 0, 0, 2), new Thickness(0, 0, 0, 2), new Thickness(0, 0, 0, 2) }
};
MyArrayData = new List<MyArrayClass>();
MyArrayData.Add(sample3);
話雖如此,我的目標是使用 ItemsControl 物件將 MyArrayInt 的內容顯示到一組 TextBlock 中(我試圖了解它是如何作業的)。特別是,我希望能夠通過系結定義每個 TextBlock 的邊框。我的 XAML 看起來像:
<ItemsControl Name="List" ItemsSource="{Binding MyArrayData}" Grid.Row="1" Grid.Column="1">
<!-- List<> -->
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl Name="MyArrayInt" ItemsSource="{Binding MyArrayInt}">
<!-- Array of int-->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}, AncestorLevel=2}, Path=MyArrayBorder}">
<TextBlock Text="{Binding Path=.}" Background="Azure" Margin="2,0,0,0"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
結果是 MyArrayInt 按我的預期顯示,但我在 BorderThickness 系結上收到以下錯誤:
System.Windows.Data Error: 40 : BindingExpression path error: 'MyArrayBorder' property not found on 'object' ''ItemsControl' (Name='List')'. BindingExpression:Path=MyArrayBorder; DataItem='ItemsControl' (Name='List'); target element is 'Border' (Name=''); target property is 'BorderThickness' (type 'Thickness')
The way I read it, it seems that the RelativeSource binding is correct: the Name of the ItemsControl object is 'List', and it's the one binding to MyArrayData. So why doesn't it find MyArrayBorder?
Any suggestion or advice is welcome!
uj5u.com熱心網友回復:
你的系結是錯誤的。您可以使用 ListBox 或 ListItem 而不是 ItemsControl,然后設定 BorderThickness="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}, AncestorLevel=2}, Path=SelectedItem.MyArrayBorder}"
uj5u.com熱心網友回復:
MyArrayClass您應該創建一個自定義型別并將它們合并為一個,而不是在 中有兩個集合屬性:
public class MyArrayClass
{
public ObservableCollection<MyType> MyArray { get; set; }
}
...
public class MyType
{
public int MyArrayInt { get; set; }
public Thickness MyArrayBorder { get; set; }
}
然后,您可以輕松系結到新型別中的每個屬性:
<ItemsControl Name="MyArrayInt" ItemsSource="{Binding MyArray}">
<!-- Array of int-->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="{Binding MyArrayBorder}">
<TextBlock Text="{Binding MyArrayInt}" Background="Azure" Margin="2,0,0,0"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
鑒于當前的設定,你需要使用一個轉換器,以便能夠從獲取特定專案MyArrayBorder的集合MyArrayClass。
您確實可以輕松系結到MyArrayBorder屬性本身:
BorderThickness="{Binding Path=MyArrayBorder[0],
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}"
...但您不能0使用純 XAML系結到動態索引處的專案(以替換上面的 snipper)。
uj5u.com熱心網友回復:
因為ItemsControl(Name='List')' 沒有MyArrayBorder屬性,但它DataContext有。
因此,要訪問您必須設定的屬性Path=DataContext.MyArrayData.MyArrayBorder。
但是正如我所看到的,您想設定BorderThikness但必須將其設定為串列中的一個條目,而不是串列。
所以像Path=DataContext.MyArrayData.MyArrayBorder[0].
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/357932.html
標籤:wpf data-binding border itemscontrol
下一篇:Dispatcher.BeginInvoke(new...ProgressBarProcess.Value=1沒有顯示?
