首页 > 其他分享 >wpf ListBox循环显示颜色选择框

wpf ListBox循环显示颜色选择框

时间:2022-11-22 01:34:26浏览次数:62  
标签:checkbox 颜色 样式 文本框 循环 设置 wpf ListBox

 数据源list,设置ListBoxItem实现

    <Page.Resources>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid Margin="5" Height="20" Width="20" MouseDown="SelectColor">
                                <Rectangle x:Name="CheckBoxRectangle" Fill="{Binding Color1}" RadiusY="5" RadiusX="5"/>
                                <Grid x:Name="CheckedMark" Width="20" Height="20" Visibility="Collapsed">
                                    <Path SnapsToDevicePixels="False" StrokeThickness="3" Data="M1,9 L10,17" Stroke="White"/>
                                    <Path SnapsToDevicePixels="False" StrokeThickness="3" Data="M8,17 L20,4" Stroke="White"/>
                                </Grid>
                            </Grid>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected"  Value="True" >
                                <Setter TargetName="CheckedMark" Property="Visibility" Value="Visible"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Page.Resources>

    <Grid>
        <StackPanel>
            <Label Margin="5,5" VerticalAlignment="Center" Background="Transparent">设置</Label>
            <StackPanel Orientation="Vertical" Name="test">
                <Label Margin="15,0,0,0">外观</Label>
                <ListView Margin="20,0,0,0">
                    <ListViewItem>
                        <DockPanel>
                            <Label>背景颜色:</Label>
                            <ListBox x:Name="backCol">
                                <ListBox.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal"></StackPanel>
                                    </ItemsPanelTemplate>
                                </ListBox.ItemsPanel>
                            </ListBox>
                        </DockPanel>
                    </ListViewItem>
                </ListView>
            </StackPanel>
        </StackPanel>
    </Grid>

参考:WPF Template简介(checkbox用Template定义样式)

WPF Grid布局(checkbox的Template样式)

WPF模板(二)应用

WPF 自定义CheckBox样式

Wpf TemplateBinding

ListBox ControlTemplate 示例(ListBox和ListBoxItem的ControlTemplate设置)

样式和模板

ps:颜色框一开始写的固定RadioButton单选框里加Rectangle,然后改成了循环显示,数据源设置为list,list循环单选框出来垂直排列的,改成水平排列又设置了一下,循环出来的单选失效了,变成每个单选框点击都选中了,另外点击后颜色就更改了,所有不用单选框也可,只需要加一个选中样式就行了,然后用checkbox样式改了一下,但是checkbox多选,网上找了设置多选框单选比较少,最后看了官网ListBox ControlTemplate 示例,改成设置ListBoxItem样式和模板了。pps,最后还想做成点击颜色第一次后出现对应颜色背景+白色√,第二次出现白色背景+对应颜色对钩,但是用ControlTemplate.Triggers实现不了,中间加了一个隐藏文本框,第一个Triggers条件 选中,设置 文本框值为1,第二个Triggers条件 选中+文本框值为1,设置文本框值为2,忘了没有执行还是直接触发了第二个还是两个Triggers调换顺序后提示System.StackOverflowException HResult=0x800703E9 Message=Exception_WasThrown

 

标签:checkbox,颜色,样式,文本框,循环,设置,wpf,ListBox
From: https://www.cnblogs.com/fangxinliu/p/16913876.html

相关文章

  • wpf 主题颜色更改
    想做成选择颜色后,更换窗体主题颜色1、在App.xaml中记录当前颜色<Application.Resources><SolidColorBrushx:Key="color1"Color="White"></SolidColorBru......
  • 【Python】for-in循环、while循环
    1.for-in循环适合用于知道循环次数的场景。计算1~100求和的结果sum=0forxinrange(101):sum+=xprint(sum)range的用法range(101):产生0~100范围的整数,......
  • 温故知新,遇见WPF/WinForms,界面过早触发,导致WPF视觉树元素未初始化出现找不到的情况(资
    报错今天在WPF中,从入口程序去引用一个类库中的资源字典样式,运行时提示找不到,但是设计时好好的,看了很久,迟迟找不到原因。内部异常1:Exception:无法找到名为“redBtn”......
  • wpf dataGrid 获取单元格,并对单元格中的对象操作
    先上图:  要求:对第一行的“选项内容举例。。。”的控件进行隐藏,如下:  前端代码:<Windowx:Class="DataGridPractice.MainWindow"xmlns="http://schema......
  • WPF 后台线程操作界面元素不卡UI界面线程 Dispatcher
    经常要考虑的,后台的耗时操作不要卡死主界面的问题。<StackPanelVerticalAlignment="Center"><Labelx:Name="lblHello">欢迎你光临WPF的世界!</Label><ButtonName......
  • 汇编loop循环
    assumecs:codecodesegmentmovcx,0ffh;循环ff次,到00就停止loopmovbx,0;初始化bx,这个是偏移地址s:movax,0hmovds,ax;确定ds段......
  • WPF布局(Layout)
    StackPanel特点:子元素一行或者一列排列<StackPanelBackground="#ECE9D8">  <TextBlockMargin="3">Lookfor:</TextBlock>  <ComboBox Margin="3"/>  <TextBlo......
  • Java实现双向循环链表
    上一篇文章实现了单向循环链表,双向很简单,在单向循环链表的基础上加一个前驱指针,节点类如下:/***双向链表节点*/publicclassNode{privateintdata;//数据......
  • shell中的for循环用法详解
    https://www.jb51.net/article/186134.htmfor命令:foriin的各种用法:foriin“file1”“file2”“file3”foriin/boot/*foriin/etc/*.confforiin......
  • WPF-Layout
    StackPanel特点:子元素一行或者一列排列<StackPanelBackground="#ECE9D8">  <TextBlockMargin="3">Lookfor:</TextBlock>  <ComboBox Margin="3"/>  <TextBlo......