覆盖ListBox中的SelectedItem背景

时间:2018-06-06 16:41:48

标签: wpf xaml listbox selecteditem

我知道这个问题已经一遍又一遍地回答,但我无法实现它。

我想覆盖我选择时显示ListboxItem的方式(如果可能的话,摆脱那个可怕的选择边框)。

我在这里和MSDN上的几个帖子中找到了实现它的方法是覆盖ItemContainerStyle中的以下画笔:

  • HighlightBrushKey
  • ControlBrushKey
  • HighlightTextBrushKey
  • InactiveSelectionHighlightBrushKey

我尝试了几种方法来实现它,但系统地失败了。更新过时了这种行为还是我执行错了?

这是我的代码:

<ListBox BorderBrush="Transparent" ItemsSource="{Binding MyItemSource}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" SelectionMode="Single">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>                                
                <Label HorizontalAlignment="Center" Margin="0 1 0 0" Foreground="Black" FontSize="14" Content="{Binding Name}"/>
            </Grid>
        </DataTemplate>      
    </ListBox.ItemTemplate>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="{Binding ColumnCount}"/> 
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Yellow" />
            </Style.Resources>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

1 个答案:

答案 0 :(得分:1)

好的,所以我终于实现了覆盖那些画笔。看起来#endif画笔不再使用了。

我覆盖 inline UNITTEST_ZAP_EXPORT static bool isActiv(void);背景的方式是定义它(大多数代码来自Blend for VS,要覆盖的模板是#define EXPORT_TEST_FUNCTIONS)。

以下是我使用的UNITTEST_ZAP_EXPORT static bool isActiv(); { return false; }

Picker pick = new Picker();
pick.setType(PICKER_TYPE_STRINGS);
pick.setStrings("First model", "Second model", "Third model");
pick.addActionListener(e->Dialog.show("Picker result", pick.getSelectedString(), "OK", null));
pick.setUseLightweightPopup(false);

Picker pick2 = new Picker();
pick2.setType(PICKER_TYPE_STRINGS);
pick2.setStrings("First model", "Second model", "Third model");
pick2.addActionListener(e->Dialog.show("Picker result", pick2.getSelectedString(), "OK", null));
pick2.setUseLightweightPopup(true);

PickerComponent pickerComponent = PickerComponent.createStrings("First model", "Second model", "Third model").label("Model Heavy");
pickerComponent.getPicker().setUseLightweightPopup(false);
pickerComponent.getPicker().addActionListener(e->Dialog.show("Picker result", pickerComponent.getPicker().getSelectedString(), "OK", null));

PickerComponent pickerComponent2 = PickerComponent.createStrings("First model", "Second model", "Third model").label("Model Light");
pickerComponent2.getPicker().setUseLightweightPopup(true);
pickerComponent2.getPicker().addActionListener(e->Dialog.show("Picker result", pickerComponent2.getPicker().getSelectedString(), "OK", null));

Form hi = new Form("PickerCompo Heavy", BoxLayout.y());
hi.add(pickerComponent);

Form hi2 = new Form("PickerCompo Light", BoxLayout.y());
hi2.add(pickerComponent2);

Form hi3 = new Form("Picker Heavy", BoxLayout.y());
hi3.add(pick);

Form hi4 = new Form("Picker Light", BoxLayout.y());
hi4.add(pick2);

Form caller = new Form("Call Hi World", BoxLayout.y());
Button callForm = new Button("hit me heavyweight Compo");
callForm.addActionListener(e->{
    hi.setBackCommand("Back", null, ev-> caller.showBack());
    hi.show();
});

Button callFormLight = new Button("hit me Lightweight Compo");
callFormLight.addActionListener(e->{
    hi2.setBackCommand("Back", null, ev-> caller.showBack());
    hi2.show();
});

Button callForm3 = new Button("hit me heavyweight");
callForm3.addActionListener(e->{
    hi3.setBackCommand("Back", null, ev-> caller.showBack());
    hi3.show();
});

Button callForm4 = new Button("hit me Lightweight");
callForm4.addActionListener(e->{
    hi4.setBackCommand("Back", null, ev-> caller.showBack());
    hi4.show();
});

caller.add(callForm);
caller.add(callFormLight);
caller.add(callForm3);
caller.add(callForm4);
caller.show();

BTW使用Blend覆盖此模板的方法: 在“对象和时间轴”面板中:右键单击SystemColor&gt;编辑其他模板&gt;编辑常规项容器(ItemContainerStyle)