简体   繁体   中英

WPF Custom ComboBox Showing "System.Windows.Styles" on item

I created a custom WPF ComboBox like this:

<ComboBox 
    x:Name="ComboBoxBtn" 
    VerticalAlignment="Top" 
    HorizontalAlignment="Left" 
    Margin="0,0,0,-1" 
    Width="300" 
    ItemsSource="{Binding Source, RelativeSource={RelativeSource AncestorType=UserControl}}"
    SelectedItem="{Binding Path=Selected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=UserControl}}"
        IsSynchronizedWithCurrentItem="True">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <ContentControl>
                    <Style TargetType="ContentControl">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Favorite}" Value="True">
                                <Setter Property="Content">
                                    <Setter.Value>
                                        <Grid>
                                            <Grid.Style>
                                                <Style TargetType="Grid">
                                                    <Setter Property="Background" Value="#FFE6E6FA"/>
                                                </Style>
                                            </Grid.Style>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto" />
                                                <ColumnDefinition Width="Auto" />
                                            </Grid.ColumnDefinitions>
                                            <Label Content="{Binding}" Width="250" />
                                            <Button Grid.Column="1" Command="{Binding AddCommandButton, ElementName=root}"
                                                CommandParameter="{Binding}">+</Button>
                                        </Grid>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Favorite}" Value="False">
                                <Setter Property="Content">
                                    <Setter.Value>
                                        <Grid>
                                            <Grid.Style>
                                                <Style TargetType="Grid">
                                                    <Setter Property="Background" Value="Yellow"/>
                                                </Style>
                                            </Grid.Style>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto" />
                                                <ColumnDefinition Width="Auto" />
                                            </Grid.ColumnDefinitions>
                                            <Label Content="{Binding}" Width="250" />
                                            <Button Grid.Column="1" Command="{Binding RemoveCommandButton, ElementName=root}"
                                                CommandParameter="{Binding}">-</Button>
                                        </Grid>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ContentControl>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

The Source binding contains a ObservableColleciton where the client has a Favorite field and is supposed to show a different background and a different button. But when I open the ComboBox all the items have:

"System.Windows.Styles"

Your problem is you are setting Style as Content in the Content Control.

<ContentControl>
    <Style TargetType="ContentControl">
    ...
    </Style>
</ContentControl>

In order to set the style of the content control you should add:

<ContentControl.Style>

So the code above will look like this:

<ContentControl>    
    <ContentControl.Style>
        <Style TargetType="ContentControl">
        ...
        </Style>    
    <ContentControl.Style>
</ContentControl>

EDIT

By the way your code is very complex for the simple task of changing the background. Here is a simplified version:

    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Grid Name="PART_GRID" Background="Yellow">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Label Content="{Binding}"
                       Width="250" />
                <Button Name="PART_BUTTON"
                        Grid.Column="1"
                        Content="-"
                        Command="{Binding AddCommandButton, ElementName=root}"
                        CommandParameter="{Binding}" />
            </Grid>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Favorite}"
                             Value="True">
                    <Setter TargetName="PART_GRID"
                            Property="Background"
                            Value="#FFE6E6FA" />
                    <Setter TargetName="PART_BUTTON"
                            Property="Content"
                            Value="+" />
                    <Setter TargetName="PART_BUTTON"
                            Property="Command"
                            Value="{Binding RemoveCommandButton, ElementName=root}" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ComboBox.ItemTemplate>

In my case, I found that I had:

<ComboBox>
    <Style>
    </Style>
</ComboBox>

which was directly adding a style as an item, which is similar to the solution above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM