从绑定传递的值作为字典中的键
Passed value from binding as key in dictionary
我尝试使用 xaml 中的字典制作 ListView 的 ListView,但我不知道该怎么做。
我有字典:
public Dictionary<string, float> dictionary {get;set;}
和
ObservableCollection<string> Parameters{get; set;}
包含所述字典中键值的名称。
我有一个 ListView
和
itemsSource = "{Binding Parameters}"
还有一个 DataTemplate
的 ListViews,其 itemsSource 类似于:
itemsSource= "{Binding dictionary[Passed_Value]}"
我无法仅使用选定的 Dictionary 值手动创建 ListView,因为用户可以选择要显示的值,并且有 10 个。
你的问题很难理解。所以,我想我会演示如何创建一个列表列表。从这个代码隐藏开始。
public class Level1
{
public int Key { get; set; }
public string Name { get; set; }
public IEnumerable<Level2> Items { get; set; }
}
public class Level2
{
public int Key { get; set; }
public string Name { get; set; }
}
public IEnumerable<Level1> Items
{
get
{
return Enumerable.Range(1, 10)
.Select(x => new Level1
{
Key = x,
Name = $"Level 1 Item {x}",
Items = Enumerable.Range(1, 10)
.Select(y => new Level2
{
Key = y,
Name = $"Level 2 Item {y}",
})
});
}
}
还有这个XAML:
<ListView ItemsSource="{x:Bind Items}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
<ItemsControl ItemsSource="{Binding Items}" Margin="12,0,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
看起来像这样:
请注意,我更改了嵌套的 ListView 并将其设为 ItemsCONtrol,因为每个都具有 SELECTED 行为的嵌套控件可能会给您带来真正的噩梦。除此之外,这就是它。不确定您是否需要字典,但绑定到字典当然是可能的 - 只是有点笨拙。
祝你好运。
或者您可能只是想知道如何使用绑定访问字典。
使用此代码:
public Dictionary<string, double> Items
{
get
{
var d = new Dictionary<string, double>();
foreach (var item in Enumerable.Range(1, 10))
{
d.Add($"Key{item}", item);
}
return d;
}
}
还有这个XAML:
<ListView ItemsSource="{x:Bind Items}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Key}" />
<Run Text="=" />
<Run Text="{Binding Value}" />
</TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
您会注意到我并没有真正使用密钥访问字典。当密钥仅通过绑定已知时,无法通过密钥访问字典。也就是说,您不能将绑定传递给绑定。
您可以执行以下操作:
<StackPanel DataContext="{Binding ElementName=ThisPage}">
<TextBlock Text="{Binding Items[Key1]}" />
</StackPanel>
但是,当然,这需要您提前知道密钥并且不绑定到它。 WinRT-XAML 中没有多重绑定。
我希望这能消除困惑。
我尝试使用 xaml 中的字典制作 ListView 的 ListView,但我不知道该怎么做。
我有字典:
public Dictionary<string, float> dictionary {get;set;}
和
ObservableCollection<string> Parameters{get; set;}
包含所述字典中键值的名称。
我有一个 ListView
和
itemsSource = "{Binding Parameters}"
还有一个 DataTemplate
的 ListViews,其 itemsSource 类似于:
itemsSource= "{Binding dictionary[Passed_Value]}"
我无法仅使用选定的 Dictionary 值手动创建 ListView,因为用户可以选择要显示的值,并且有 10 个。
你的问题很难理解。所以,我想我会演示如何创建一个列表列表。从这个代码隐藏开始。
public class Level1
{
public int Key { get; set; }
public string Name { get; set; }
public IEnumerable<Level2> Items { get; set; }
}
public class Level2
{
public int Key { get; set; }
public string Name { get; set; }
}
public IEnumerable<Level1> Items
{
get
{
return Enumerable.Range(1, 10)
.Select(x => new Level1
{
Key = x,
Name = $"Level 1 Item {x}",
Items = Enumerable.Range(1, 10)
.Select(y => new Level2
{
Key = y,
Name = $"Level 2 Item {y}",
})
});
}
}
还有这个XAML:
<ListView ItemsSource="{x:Bind Items}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
<ItemsControl ItemsSource="{Binding Items}" Margin="12,0,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
看起来像这样:
请注意,我更改了嵌套的 ListView 并将其设为 ItemsCONtrol,因为每个都具有 SELECTED 行为的嵌套控件可能会给您带来真正的噩梦。除此之外,这就是它。不确定您是否需要字典,但绑定到字典当然是可能的 - 只是有点笨拙。
祝你好运。
或者您可能只是想知道如何使用绑定访问字典。
使用此代码:
public Dictionary<string, double> Items
{
get
{
var d = new Dictionary<string, double>();
foreach (var item in Enumerable.Range(1, 10))
{
d.Add($"Key{item}", item);
}
return d;
}
}
还有这个XAML:
<ListView ItemsSource="{x:Bind Items}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Key}" />
<Run Text="=" />
<Run Text="{Binding Value}" />
</TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
您会注意到我并没有真正使用密钥访问字典。当密钥仅通过绑定已知时,无法通过密钥访问字典。也就是说,您不能将绑定传递给绑定。
您可以执行以下操作:
<StackPanel DataContext="{Binding ElementName=ThisPage}">
<TextBlock Text="{Binding Items[Key1]}" />
</StackPanel>
但是,当然,这需要您提前知道密钥并且不绑定到它。 WinRT-XAML 中没有多重绑定。
我希望这能消除困惑。