我設法進一步得到我只讀了一下休息後複選框,現在我想在一個合理的優雅形態的功能。問題是我使用了一些黑客來使其工作,雖然這不是一場災難,但它會更好地做到這一點。添加自定義依賴屬性來控制模板在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的標籤。
所以我想我的問題是:
- 有我的堅持錯誤的結束?是否有一個地方可以重寫任何機制導致盒子自檢的機制?也許在ControlTemplate觸發器?
- 這是個好主意來繞去消除任何備用XAML,我覺得只是被從默認的CheckBox帶來的還是應該儘量保持完整替代所有樣式?
- 如果我做的是不是太瘋狂了,我可以添加在XAML一個依賴屬性,這樣我就不必使用Tag屬性?
- 它也發生,我認爲也許是我真正想要的是一個按鈕控件,看起來像一個複選框,也許在頂部通常動畫複選框,我將數據綁定到的圖形一種無形的按鈕。對這一計劃的任何想法也將非常受歡迎。
非常感謝
埃德