简体   繁体   中英

How do I change a controls background on my UserControl when the item is selected

So I have this ListView which has a DataTemplate of my UserContol because I wanted a custom design for my ListView and it looks like this

<ListView x:Name="LeftMenuListView"
          ItemsSource="{Binding MenuItems}"
          SelectedItem="{Binding SelectedMenuItem}"
          BorderThickness="0"
          Width="255">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <local:MenuItemControl/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Super simple, now when an Item is selected the entire thing changes color which I want it looks great imo

<Style TargetType="ListViewItem">
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border
                        Name="Border"
                        BorderThickness="0">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="Border" Property="Background"
                                    Value="#444444"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

But there is a border inside my usercontrol thats 10px wide with the name SmallBorder. I want to change the color of that to green when the item is selected but I have no idea how to access that property

My UserControl

<Grid Background="Transparent">
    <TextBlock Text="{Binding Name}"
               VerticalAlignment="Center"
               Margin="20,0,0,0"
               Foreground="#9e9e9e"
               FontFamily="Tahoma"/>

    <Border Width="10"
            HorizontalAlignment="Left"
            x:Name="SmallBorder"/>
</Grid>

So how do I change the color of SmallBorder when an item is selected and then when it's not selected it turns transparent?

The ViewModel, which is the DataContext of you usercontrol, should expose a property like IsSelected, then you can add an style with a DataTrigger that reacts to a change in this property.

EDIT:

Declare an style for the border itself an access it as an StaticResource: It could be placed in a ResourceDictionary, within YourUserControl.Resources or inline with the Border control declaration:

<Style TargetType={x:Type Border} x:Key=SelectedBorderStyle>
    <Style.Triggers>
       <DataTrigger Binding="{Binding IsSelected}" Value="True">
           <Setter Property="BorderBrush" Value="Green" />
       </DataTrigger>
    </Style.Triggers>
</Style>

And then your UserControl would be:

<Grid Background="Transparent">
    <TextBlock Text="{Binding Name}"
               VerticalAlignment="Center"
               Margin="20,0,0,0"
               Foreground="#9e9e9e"
               FontFamily="Tahoma"/>

    <Border Width="10"
            Style={StaticResource SelectedBorderStyle}
            HorizontalAlignment="Left"/>
</Grid>

Note that now you don't need to set the name for the Border.

A Border is invisible unless there is something in it, but you could replace the Border with a Grid and use a Style with a DataTrigger that binds to the IsSelected property:

<Grid Background="Transparent">
    <TextBlock Text="{Binding Name}"
               VerticalAlignment="Center"
               Margin="20,0,0,0"
               Foreground="#9e9e9e"
               FontFamily="Tahoma"/>

    <Grid Width="10"
          HorizontalAlignment="Left"
          x:Name="SmallBorder">
        <Grid.Style>
            <Style TargetType="Grid">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListViewItem}}" Value="True">
                        <Setter Property="Background" Value="Red" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>
    </Grid>
</Grid>

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