首页 > 其他分享 >【WPF控件样式】

【WPF控件样式】

时间:2024-07-12 15:42:59浏览次数:16  
标签:控件 CheckBox 自定义 单选框 样式 WPF

自定义弹窗单选框

CheckBox扁平化

<Style x:Key="CheckBoxStyle1" TargetType="{x:Type CheckBox}">
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}">
                <Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                        <Grid x:Name="markGrid" Width="16" Height="16">
                            <Path x:Name="optionMark" Data="M853.32992 102.4H170.67008C133.13024 102.4 102.4 133.13024 102.4 170.67008v682.65984C102.4 890.86976 133.13024 921.6 170.67008 921.6h682.65984C890.86976 921.6 921.6 890.86976 921.6 853.32992V170.67008C921.6 133.13024 890.86976 102.4 853.32992 102.4zM457.8304 716.8L204.8 463.72864l48.27136-48.26112 204.75904 204.8L770.92864 307.2 819.2 355.47136 457.8304 716.8z"
                                  Fill="#787878" Margin="1" Stretch="Fill"/>
                        </Grid>
                    <ContentPresenter x:Name="contentPresenter" Grid.Column="1" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasContent" Value="true">
                        <Setter Property="Padding" Value="4,-1,0,0"/>
                    </Trigger>
               
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Fill" TargetName="optionMark" Value="#1296db"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

ListBox去除交互效果

<Style x:Key="ListBoxItemContainerStyle1" TargetType="{x:Type ListBoxItem}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="Padding" Value="4,1"/>
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

以上两者配合

<Popup Name="MyPopup" StaysOpen="False" PlacementTarget="{Binding ElementName=ShowPopupButton}" AllowsTransparency="True" PopupAnimation="Slide">
    <Border Background="#484848"  BorderThickness="0" CornerRadius="5" Padding="5">
        <ListBox Name="MyListBox" BorderThickness="0" Background="Transparent"
                 ItemsSource="{Binding TimeSeletList}"
                  ItemContainerStyle="{StaticResource ListBoxItemContainerStyle1}"
                 >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox Content="{Binding }"  Style="{StaticResource CheckBoxStyle1}" Foreground="White"
                              IsChecked="{Binding IsSelected, 
                                          RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, 
                                          Mode=TwoWay}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Border>
</Popup>

image

标签:控件,CheckBox,自定义,单选框,样式,WPF
From: https://www.cnblogs.com/flashing-magic/p/18298520

相关文章

  • WPF中引用不到相对路径图片?
    在wpf中使用相对路径运行项目时却不显示图片怎么解决?新建img文件夹添加所需要的图片选中图片右键属性设置属性重新生成即可运行效果转载请标明出处!......
  • 解决若依框架与tailwindcss 样式冲突问题
    首先下载tailwindcss样式https://www.tailwindcss.cn/docs/installation我们可以按照官方文档来进行配置 npminstall-Dtailwindcssnpxtailwindcssinit /**@type{import('tailwindcss').Config}*/module.exports={content:["./src/**/*.{html,js}"],the......
  • 演示:【Avalonia-Controls】Avalonia皮肤,主题,自定义控件,数据库,系统模块资源库
    一、目的:分享一个Avalonia皮肤,主题,自定义控件,数据库,系统模块资源库开源地址:GitHub-HeBianGu/Avalonia-Controls:Avalonia控件库Nuget包地址:NuGetGallery|PackagesmatchingHeBianGu.AvaloniaUI.演示视频地址:【Avalonia-Controls】Avalonia工具组件皮肤库v1.0.0_......
  • WPF WebBrowser navigate to website via url and escape script error warning
    Copyfrom https://www.iditect.com/faq/csharp/wpf-webbrowser-control--how-to-suppress-script-errors.html#:~:text=To%20suppress%20these%20script%20errors%2C%20you%20can%20handle,using%20the%20Cancel%20property%20of%20the%20WebBrowserNavigatingEventArgs%20pa......
  • WPF display and host pdf via WebBrowser
    //xaml<Windowx:Class="WpfApp206.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF WebBrowser make sure the path or Internet address is correct
      Onepossiblecauseisincludechinesecharacters, //WrongcodeprivatevoidOpenClick(objectsender,RoutedEventArgse){OpenFileDialogdialog=newOpenFileDialog();dialog.Filter="PDFFiles|*.pdf|AllFiles|*.*";i......
  • 【QT】容器类控件
    容器类控件1.GroupBox2.TabWidget1.GroupBox使用QGroupBox实现⼀个带有标题的分组框。可以把其他的控件放到里面作为⼀组。这样看起来能更好看⼀点。注意,不要把QGroupBox和QButtonGroup混淆.(之前在介绍QRadionButton的时候提到了QButtonGroup)......
  • Simple WPF: S3实现MINIO大文件上传并显示上传进度
    最新内容优先发布于个人博客:小虎技术分享站,随后逐步搬运到博客园。创作不易,如果觉得有用请在Github上为博主点亮一颗小星星吧!目的早两天写了一篇S3简单上传文件的小工具,知乎上看到了一个问题问如何实现显示MINIO上传进度,因此拓展一下这个小工具能够在上传大文件时显示进度。完......
  • C# Winform之propertyGrid控件分组后排序功能
    在WinForms的PropertyGrid控件中,你可以通过多种方式对属性进行排序,包括按类别(Category)排序以及按属性名称排序。默认情况下,PropertyGrid控件会根据[Category]和[DisplayName]属性装饰器对属性进行分组和排序。如果你想要自定义排序规则,你可以通过以下几种方法:使用......
  • C# Winform之propertyGrid控件使用详解和分组设置
    PropertyGrid控件在WinForms中是一个非常有用的工具,它允许用户查看和编辑一个对象的属性。这个控件非常适合用于配置对话框或任何需要动态显示对象属性的地方。下面我会详细介绍PropertyGrid的使用方法和如何对属性进行分组。使用详解1.添加 PropertyGrid 控件在Vi......