2009-07-06 124 views
4

我设法进一步得到我只读了一下休息后复选框,现在我想在一个合理的优雅形态的功能。问题是我使用了一些黑客来使其工作,虽然这不是一场灾难,但它会更好地做到这一点。添加自定义依赖属性来控制模板在XAML

总结一下:我想经常看复选框,单击时是不自检,而不是点击事件触发后台工作,后来就导致一个变量来进行更新。该变量绑定到checkbox.ischecked,然后用新值更新。

我想用基于这样的理念在这里控件模板:

A read-only CheckBox in C# WPF

我已经修改了这一点,并剥离出来的东西,我想我并不需要(也许是不明智),并结束了:如上所述

<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> 

此复选框的作品,我这样称呼它:

<CheckBox x:Name="uiComboBox" Content="Does not set the backing property, but responds to it." 
        Style="{StaticResource ReadOnlyCheckBoxStyle}" Tag="{Binding MyBoolean}" Click="uiComboBox_Click"/> 

我做的破解是使用'标记'DependencyProperty将数据绑定到控制模板中。这绕过了通常导致复选框自检的任何机制。要恢复到正常的演技复选框只是改变结合标记的结合和器isChecked的BulletDecorator内设置TemplateBinding来代替器isChecked的标签。

所以我想我的问题是:

  1. 有我的坚持错误的结束?是否有一个地方可以重写任何机制导致盒子自检的机制?也许在ControlTemplate触发器?
  2. 这是个好主意来绕去消除任何备用XAML,我觉得只是被从默认的CheckBox带来的还是应该尽量保持完整替代所有样式?
  3. 如果我做的是不是太疯狂了,我可以添加在XAML一个依赖属性,这样我就不必使用Tag属性?
  4. 它也发生,我认为也许是我真正想要的是一个按钮控件,看起来像一个复选框,也许在顶部通常动画复选框,我将数据绑定到的图形一种无形的按钮。对这一计划的任何想法也将非常受欢迎。

非常感谢

埃德

回答

6

我设法弄清楚这个问题,我的ReadOnlyCheckBox的想法,到底我创建了一个自定义的控制根据各地按钮,然后应用风格,使它看起来像一个CheckBox。我添加了自己的IsChecked属性,该属性在用户单击时未设置,但绑定到数据,因此显示的检查仅在数据更改时才显示。

C#:

public class ReadOnlyCheckBoxControl : System.Windows.Controls.Button 
{ 
    public static DependencyProperty IsCheckedProperty; 

    public ReadOnlyCheckBoxControl() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(ReadOnlyCheckBoxControl), new FrameworkPropertyMetadata(typeof(ReadOnlyCheckBoxControl))); 
    } 

    public bool IsChecked 
    { 
     get { return (bool)GetValue(IsCheckedProperty); } 
     set { SetValue(IsCheckedProperty, value); } 
    } 

    static ReadOnlyCheckBoxControl() 
    { 
     IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(ReadOnlyCheckBoxControl)); 
    } 
} 

XAML:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:y="clr-namespace:ReadOnlyCheckBoxControlNS;assembly=" 
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"> 

<SolidColorBrush x:Key="CheckBoxFillNormal" Color="#F4F4F4" /> 
<SolidColorBrush x:Key="CheckBoxStroke" Color="#8E8F8F" /> 

<Style x:Key="EmptyCheckBoxFocusVisual"> 
    <Setter Property="Control.Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Rectangle SnapsToDevicePixels="true" 
          Margin="1" 
          Stroke="Black" 
          StrokeDashArray="1 2" 
          StrokeThickness="1" /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style x:Key="CheckRadioFocusVisual"> 
    <Setter Property="Control.Template"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Rectangle SnapsToDevicePixels="true" 
          Margin="14,0,0,0" 
          Stroke="Black" 
          StrokeDashArray="1 2" 
          StrokeThickness="1" /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style TargetType="{x:Type y:ReadOnlyCheckBoxControl}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type y:ReadOnlyCheckBoxControl}"> 
       <BulletDecorator SnapsToDevicePixels="true" Background="Transparent"> 
        <BulletDecorator.Bullet> 
         <Microsoft_Windows_Themes:BulletChrome Background="{StaticResource CheckBoxFillNormal}" 
                   BorderBrush="{StaticResource CheckBoxStroke}" 
                   RenderMouseOver="{TemplateBinding IsMouseOver}" 
                   IsChecked="{TemplateBinding IsChecked}"> 
         </Microsoft_Windows_Themes:BulletChrome> 
        </BulletDecorator.Bullet> 
        <ContentPresenter SnapsToDevicePixels="True" 
             HorizontalAlignment="Left" 
             Margin="4,0,0,0" 
             VerticalAlignment="Center" 
             RecognizesAccessKey="True" /> 
       </BulletDecorator> 
       <ControlTemplate.Triggers> 
        <Trigger Property="HasContent" Value="true"> 
         <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}" /> 
         <Setter Property="Padding" Value="4,0,0,0" /> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}" /> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style>