2009-07-06 122 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>