[英]Listbox with dictionary, selectedItem on initial page load
I have a dictionary which takes a string and a double, and I'm trying to get a selected element of the dictionary to be automatically selected (and highlighted) when I open the page that contains the listbox. 我有一个包含字符串和双精度字的字典,当我打开包含列表框的页面时,我试图使字典中的所选元素自动选择(并突出显示)。 I can get this working with a list of strings, but can't get it working with the dictionary.
我可以使它与字符串列表一起工作,但不能与字典一起工作。
<UserControl.Resources>
<Style x:Key="MyLbStyle"
TargetType="{x:Type ListBox}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Yellow" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey }"
Color="Yellow" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}"
Color="Black" />
</Style.Resources>
</Style>
</UserControl.Resources>
<DockPanel Height="Auto"
Width="Auto">
<ListBox ItemsSource="{Binding MyDict, Mode=OneWay}"
Style="{ StaticResource MyLbStyle}"
DockPanel.Dock="Top"
Height="130"
SelectedValuePath="Key"
SelectedValue="{Binding SelectedVal, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
FontSize="18"
Margin="0,0,0,20">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding Key, Mode=OneWay}" />
<TextBlock Text="{Binding Value, Mode=OneWay}" />
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
And the VM 和虚拟机
public class NumericParameterPageViewModel : ActionSelectionPage
{
private readonly Dictionary<string, IMyInterface> _valStorage = new Dictionary<string, IMyInterface>();
private IMyInterface _selectedParameter;
public ListBoxPage()
{
}
public IMyInterface MyItem
{
get { return _selectedParameter; }
set
{
SetProperty( ref _selectedParameter, value );
}
}
public string SelectedVal
{
get
{
return _x;
}
set
{
SetProperty( ref _x, value );
MyItem = _valStorage[ _x ];
}
}
private string _x;
public Dictionary<string, double> MyDict { get; } = new Dictionary<string, double>();
public override void OnNavigatedTo(
NavigationContext navigationContext )
{
var p = navigationContext.Parameters[ "ParameterList" ] as IMyInterface[];
foreach (var item in p)
{
MyDict.Add( item.ID.Name, item.CurrentValue );
_valStorage.Add( item.ID.Name, item );
}
SelectedItem = navigationContext.Parameters["ParamterID"] as IMyInterface;
SelectedVal = MyDict.First().Key;
base.OnNavigatedTo( navigationContext );
}
}
All interface names here are made up, if two are slightly different (binding wise) it's because I cocked up manually changing them for the example. 这里的所有接口名称都是伪造的,如果两个稍有不同(明智的绑定),那是因为我为示例手动调整了接口名称。
Although it actually contains much more, for now assume IMyInterface just contains a string name, and a double value. 尽管它实际上包含更多内容,但现在假设IMyInterface仅包含一个字符串名称和一个双精度值。 (I apologize for the messy code / naming, still prototyping this)
(我为混乱的代码/命名表示歉意,仍对此进行原型制作)
You should set the Style
(and not the ItemContainerStyle
) property to a ListBox
style: 您应该将
Style
(而不是ItemContainerStyle
)属性设置为ListBox
样式:
<Style x:Key="MyLbStyle" TargetType="{x:Type ListBox}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Yellow" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey }"
Color="Yellow" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}"
Color="Black" />
</Style.Resources>
</Style>
<ListBox ItemsSource="{Binding MyDict, Mode=OneWay}"
Style="{StaticResource MyLbStyle}"
DockPanel.Dock="Top"
SelectedValuePath="Key"
SelectedValue="{Binding SelectedVal, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding Key, Mode=OneWay}" />
<TextBlock Text="{Binding Value, Mode=OneWay}" />
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.