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

前述したように、このチェックボックスは動作し、私はこのようにそれを呼び出す:

私が作ったハックは、コントロールテンプレートにデータバインディングを運ぶために 'Tag' DependencyPropertyを使用することでした。これは、通常、チェックボックスに自己チェックをさせるメカニズムをバイパスします。通常の動作チェックボックスに戻すには、TagへのバインディングをIsCheckedへのバインディングに変更し、BulletDecorator内でTagBindingをTagの代わりにIsCheckedに設定します。

だから、私の質問があると思います:私はスティックの間違った終わりを持っている

  1. ?ボックスが自己チェックする原因となるメカニズムを無効にすることができる場所はありますか?おそらくControlTemplateのトリガですか?
  2. デフォルトのチェックボックスから取り込んだだけのスペアXAMLを削除することをお勧めしますか、またはすべてのスタイルを完全に置き換えようとする必要がありますか?
  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>