我有一Binding堂課List<KeyValuePair<BoardElement,int>>:
public class Binding
{
public List<KeyValuePair<BoardElement, int>> Outputs { get; set; }
}
并且類BoardElement包含屬性Name:
public class BoardElement
{
private string Name { get; set; }
}
我有ObservableCollection<Binding>。
現在,我想要Binding Name<- BoardElement<- KeyValuePair<- Outputs。
我嘗試這樣的事情
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Outputs.Key.Name}" SelectedIndex="{Binding SelectedType}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
但這不起作用,因為它需要在此處的路徑中添加一些東西Binding Outputs.?.Key.Name。只有當我選擇第一個元素時它才有效Binding Outputs[0].Key.Name,但我想TextBlock為每個元素創建一個KeyValuePair并為他們帶來一個Name。
public ObservableCollection<Binding> B { get; set; }
public MainWindow()
{
var a = new Binding();
a.Input = new BoardElement("D1");
a.Outputs = new List<KeyValuePair<BoardElement, int>>();
var c = new BoardElement("D2");
a.Outputs.Add(new KeyValuePair<BoardElement, int>(c, 7));
B = new ObservableCollection<Binding>();
B.Add(a);
ListOfBindngs.ItemsSource = B;
}
所以我想知道Binding另一個集合中的collections如何?我只能按索引([0]、[1] 等...)進行選擇,但我想全部顯示
uj5u.com熱心網友回復:
您有一個嵌套串列: list ofBinding并且每個串列都有一個KeyValuePair.
選項 A,嵌套串列框:
<ListBox SelectedIndex="{Binding SelectedType}" x:Name="ListOfBindngs" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ListBox ItemsSource="{Binding outputs}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Key.Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
選項 B,轉換器:
public class ConvertOutputs : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var list = (List<KeyValuePair<BoardElement, int>>)value;
return string.Join(", ", list.Select(x => x.Key.Name));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => null;
}
在 xaml 中:
<ListBox SelectedIndex="{Binding SelectedType}" x:Name="ListOfBindngs" >
<ListBox.Resources>
<local:ConvertOutputs x:Key="ConvertOutputs"/>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding outputs, Converter={StaticResource ConvertOutputs}}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/453554.html
上一篇:根據Viewbag條件使KendoASP.NETMVCinCell可編輯
下一篇:更改時自定義文本框不更新屬性
