WPF ListBox选中项样式更改

简介:

        修改ListBox 选中Item后的效果,通过显式设置 ItemContainerStyle 属性。

 

介绍:

 ItemContainerStyle 获取或设置应用于为每个项生成的容器元素的 Style。 

 

使用:

更改选中项背景色。

添加提示。

 public class PersonNP : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string name;
        public string Name
        {
            get { return name;  }
            set { name = value; Notify(); }
        }

        private string id;
        public string Id
        {
            get { return id; }
            set { id = value;Notify(); }
        }
         
        private void Notify([CallerMemberName]string obj ="")
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged (this, new PropertyChangedEventArgs(obj));
            }
        }
    }

 

        public MainWindow()
        {
            InitializeComponent();

            ObservableCollection<PersonNP> ocP = new ObservableCollection<PersonNP>() {
            new PersonNP(){Id = "1", Name="test1"},
            new PersonNP(){Id = "2", Name="test2"},
            };

            listBx.ItemsSource = ocP;      
        }
   <Window.Resources>
        <ToolTip x:Key="InforTip">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Id:" />
                <TextBlock Text="{Binding Path=Id}"/>
                <TextBlock Text=" Name:" />
                <TextBlock Text="{Binding Path=Name}"/>
            </StackPanel>
        </ToolTip>
      <Style TargetType="ListBox" x:Key="lbxS">
            <Style.Setters>
                <Setter Property="FontSize" Value="20"/>
                <Setter Property="ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Border Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=Background}"
                                    Margin="5 5 5 5" CornerRadius="5" BorderThickness="2" BorderBrush="LimeGreen">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition/>
                                        <RowDefinition/>
                                    </Grid.RowDefinitions>
                                    <TextBlock Text="{Binding Id}"/>
                                    <TextBlock Text="{Binding Name}" Grid.Row="1"/>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ItemContainerStyle">
                    <Setter.Value>
                        <Style TargetType="ListBoxItem">
                            <Style.Triggers>
                                <Trigger Property="ListBoxItem.IsSelected" Value="True">
                                    <Setter Property="ListBoxItem.Background" Value="Green"/>
                                </Trigger>
                                <Trigger Property="ListBoxItem.IsMouseOver" Value="True">
                                    <Setter Property="ListBoxItem.ToolTip" Value="{Binding Source={StaticResource InforTip}}" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>
    </Window.Resources>

    <Grid>
        <StackPanel>
            <ListBox x:Name="listBx" Style="{StaticResource lbxS}" ></ListBox>
        </StackPanel>
    </Grid>

 

 

效果:

              

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要修改ListBox样式,可以使用ListBox.ItemContainerStyle属性来设置,具体步骤如下: 1. 在ListBox的属性列表中找到ItemContainerStyle属性,点击它右侧的“编辑模板”按钮,选择“编辑额外模板”->“编辑模板”。 2. 在弹出的窗口中,可以看到ListBox的默认样式,可以在上面进行修改。 3. 修改完成后,点击窗口上方的“应用”按钮即可预览效果。 4. 最后,点击窗口底部的“确定”按钮保存修改。 如果只是想修改选中的背景颜色,可以使用ListBox控件的SelectionBrush属性来设置。例如,将选中的背景颜色设置为红色: ```xml <ListBox SelectionBrush="Red"> <ListBoxItem>Item 1</ListBoxItem> <ListBoxItem>Item 2</ListBoxItem> <ListBoxItem>Item 3</ListBoxItem> </ListBox> ``` 如果希望自定义选中样式,可以使用ListBox控件的ItemContainerStyle属性和Trigger来实现。例如,将选中的背景颜色设置为蓝色,文字颜色设置为白色: ```xml <ListBox> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border Name="border" Background="Transparent" BorderBrush="Transparent" BorderThickness="1"> <ContentPresenter /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter TargetName="border" Property="Background" Value="Blue" /> <Setter Property="Foreground" Value="White" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.ItemContainerStyle> <ListBoxItem>Item 1</ListBoxItem> <ListBoxItem>Item 2</ListBoxItem> <ListBoxItem>Item 3</ListBoxItem> </ListBox> ``` 以上代码会将选中的背景颜色设置为蓝色,文字颜色设置为白色。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值