小编EdW*_*ugh的帖子

将自定义依赖项属性添加到XAML中的控制模板

经过一段时间的休息后,我已经设法进一步使用了我的只读复选框,现在我以理想的优雅形式拥有了我想要的功能.问题是我已经使用了一些黑客来使它工作,虽然这不是一个灾难,它会更好地做到这一点.

回顾一下:我想要一个常规查看复选框,在单击它时不会自我检查,而是单击事件会触发后台工作程序,稍后会导致更新变量.此变量绑定到checkbox.ischecked,然后使用新值更新.

我想在这里使用基于这个想法的控件模板:

C#WPF中的只读CheckBox

我修改了这个并剥离了我认为我不需要的东西(也许是不明智的)并最终得到:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
<!-- -->
<Style x:Key="ReadOnlyCheckBoxStyle" TargetType="{x:Type CheckBox}" >
        <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}">
                <BulletDecorator SnapsToDevicePixels="true" Background="Transparent">
                    <BulletDecorator.Bullet>
                        <Microsoft_Windows_Themes:BulletChrome Background="{TemplateBinding Background}"
                                                               BorderBrush="{TemplateBinding BorderBrush}"
                                                               RenderMouseOver="{TemplateBinding IsMouseOver}"
                                                               IsChecked="{TemplateBinding Tag}">
                        </Microsoft_Windows_Themes:BulletChrome>
                    </BulletDecorator.Bullet>
                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      Margin="{TemplateBinding Padding}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      RecognizesAccessKey="True" />
                </BulletDecorator>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

这个复选框的工作方式如上所述,我这样称呼它:

<CheckBox x:Name="uiComboBox" Content="Does not set the backing property, but responds to it." 
                  Style="{StaticResource ReadOnlyCheckBoxStyle}" …
Run Code Online (Sandbox Code Playgroud)

c# checkbox xaml binding controltemplate

4
推荐指数
1
解决办法
5590
查看次数

标签 统计

binding ×1

c# ×1

checkbox ×1

controltemplate ×1

xaml ×1