带有字典的列表框,初始页面加载时为selectedItem

时间:2017-04-11 08:45:25

标签: c# wpf

我有一个字典,它带有一个字符串和一个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值。 (我为凌乱的代码/命名道歉,仍然是原型)

1 个答案:

答案 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>