4

WPFでチェックボックスをチェックできないようにしたい(C#コードから)。チェックを外すことだけが許可されます。
どうすればいいですか?

PS。チェックボックスをオンにした後すぐにチェックボックスをオフにするClick イベント
のイベント ハンドラーを記述することは受け入れられません。

[編集]
ユーザーがチェックボックスをチェックできると考えることができるように、チェックボックスは常に有効にする必要があります。奇妙ですが、それは特定のプログラムです。

4

3 に答える 3

9
<CheckBox Content="Checkbox"
          IsEnabled="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}" />

チェックボックスは、そうでない場合でも、常にチェック可能な状態に見える必要があることを通知された後に編集します。これが実際の例です:

<Window
        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" x:Class="WpfTest.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ControlTemplate x:Key="DeceptiveCheckbox" TargetType="{x:Type CheckBox}">
            <BulletDecorator Background="Transparent" SnapsToDevicePixels="True">
                <BulletDecorator.Bullet>
                    <Microsoft_Windows_Themes:BulletChrome BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" IsChecked="{TemplateBinding IsChecked}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"/>
                </BulletDecorator.Bullet>
                <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </BulletDecorator>
            <ControlTemplate.Triggers>
                <Trigger Property="HasContent" Value="True">
                    <Setter Property="FocusVisualStyle">
                        <Setter.Value>
                            <Style>
                                <Setter Property="Control.Template">
                                    <Setter.Value>
                                        <ControlTemplate>
                                            <Rectangle Margin="14,0,0,0" SnapsToDevicePixels="True" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Padding" Value="4,0,0,0"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
        <CheckBox Content="CheckBox"  IsEnabled="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Template="{StaticResource DeceptiveCheckbox}" />
    </Grid>
</Window>

修正は簡単でした。現在のチェックボックス テンプレートのコピーを作成して削除しただけです。

<Trigger Property="IsEnabled" Value="False">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
于 2012-06-13T19:23:53.900 に答える
3

これはどう:

<Grid>
    <Grid.Resources>
        <Style TargetType="CheckBox">
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="False">
                    <Setter Property="IsEnabled" Value="False" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>

    <CheckBox IsChecked="True" />
</Grid>

x:Keyこのアプローチには、特別なことをしたり、個々のチェックボックスのバインディングを変更したりする必要なく、すべて (そのまま) または多く (属性を適用してからリソースを目的のチェックボックスに割り当てる場合) に適用できるという利点があります。

于 2012-06-13T19:25:45.190 に答える
2

簡単な解決策は、IsEnabled プロパティを IsChecked プロパティと等しくなるようにバインドすることです。

于 2012-06-13T19:22:08.910 に答える