我有一个字典,它带有一个字符串和一个double,当我打开包含列表框的页面时,我试图让字典的选定元素自动选中(并突出显示)。我可以使用字符串列表来处理这个问题,但是无法使用字典。
const autoPopulateCellnumber = (getState) => {
const cell = getState().getIn(['answers','owner-contact-number'])
const type = types.AUTO_POPULATE_CELLNUMBER;
return {
type,
payload: { cell } ,
};
};
和VM
<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>
如果两个稍微不同(绑定方式),那么这里的所有接口名称都会组成,因为我为了示例而手动更改了它们。
虽然它实际上包含更多,但现在假设IMyInterface只包含一个字符串名称和一个double值。 (我为凌乱的代码/命名道歉,仍然是原型)
答案 0 :(得分:1)
您应该将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>