カスタムControlTemplateでRadioButtonでIscheckedをバインドできません

StackOverflow https://stackoverflow.com/questions/5381786

  •  28-10-2019
  •  | 
  •  

質問

私は、コントロールテンプレートとしてToggleButtonを備えたカスタムラジオボタンコントロールを使用しています。 XAMLがどのように見えるかは次のとおりです。

    <RadioButton.Template>
        <ControlTemplate>
            <ToggleButton x:Name="tb" IsChecked="{Binding IsChecked, Mode=TwoWay, 
                                RelativeSource={RelativeSource TemplatedParent}}" 
                                      Content="{TemplateBinding RadioButton.Content}"
                          PreviewMouseDown="tb_PreviewMouseDown">
            </ToggleButton>
        </ControlTemplate>
    </RadioButton.Template>

ボタンのiScheckedプロパティをプログラム的に設定しようとするか、それと拘束する場合を除き、それはうまく機能しています。その後、チェックする必要があるボタンは視覚的に反応しません - 押されているようには見えず、エアロマウスオーバーエフェクトは表示されません。クリックされたイベントハンドラーは引き続き機能し、RadioButtonとControlTemplateのトグルボタンの両方のIscheckedプロパティは、それらの値を調べるときにTRUEです。エイミー私はバインディングに何か間違ったことをしていますか?何か案は?

アプリケーションで使用する方法の例は次のとおりです。

<local:RadioToggleButton Content="1Hr" GroupName="Interval" x:Name="oneHrBtn" 
IsChecked="{BindingPath=oneHrBtnIsChecked, Mode=TwoWay}" Margin="2 5 3 5" 
IsEnabled="{Binding Path=oneHrBtnIsEnabled, Mode=TwoWay}"/>
役に立ちましたか?

解決

したがって、私のカスタムRadioToggleButtonコントロールの問題は、実際に非常に奇妙なものによって引き起こされていました。以下の解決策について説明します。他の誰かがこの特定の問題に遭遇することを期待しているからではなく、問題とは関係がないと思われる解決策の例として説明します。

ボタングループを含むGroupBoxのISENABLEDプロパティに拘束力がありました。この結合は正常に動作し、必要に応じてすべての内部統制を有効にし、無効にしたように見えました。しかし、この結合を削除するとすぐに、上記の問題は消えました。これは理想的ではありませんが、私はこの問題に時間がかかりすぎたと判断したので、個々のコントロールのISENabledプロパティをグループボックスが縛られていたのと同じプロパティに縛り付けられています。欲しかった。

他のヒント

あなたが持っているものはとても奇妙です。 RadioButtonクラスは、ToggleButtonに由来します。そのため、ボタンにボタンを入れます。あなたは単にラジオボタンをトグルボタンのように見せようとしていますか?もしそうなら、ToggleButtonを直接使用してみませんか?

GroupName機能を使用できるようにRadioButtonをToggleButtonのように見せたい場合は、ToggleButton Controlテンプレートをコピーして使用する必要があります(コントロールテンプレートにトグルボタンを埋め込まれていません)。

デフォルトのテンプレートを取得できます ここ. 。次に、ToggleButtonスタイルを検索し、ControlTemplateをコピーします。

編集:

次の例は、これがどのように行われるかを示しています。 PresentionFramework.aeroへの参照を追加するだけです。

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>

        <LinearGradientBrush x:Key="ButtonNormalBackground" StartPoint="0,0" EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#F3F3F3" Offset="0" />
                <GradientStop Color="#EBEBEB" Offset="0.5" />
                <GradientStop Color="#DDDDDD" Offset="0.5" />
                <GradientStop Color="#CDCDCD" Offset="1" />
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>
        <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070" />

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

        <Style x:Key="{x:Type RadioButton}" TargetType="{x:Type RadioButton}">
            <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}" />
            <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}" />
            <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
            <Setter Property="HorizontalContentAlignment" Value="Center" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="Padding" Value="1" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RadioButton}">
                        <theme:ButtonChrome Name="Chrome" Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding Button.IsDefaulted}"
                                RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"
                                SnapsToDevicePixels="true">
                            <ContentPresenter Margin="{TemplateBinding Padding}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </theme:ButtonChrome>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsKeyboardFocused" Value="true">
                                <Setter TargetName="Chrome" Property="RenderDefaulted" Value="true" />
                            </Trigger>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter TargetName="Chrome" Property="RenderPressed" Value="true" />
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="#ADADAD" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <StackPanel>
        <RadioButton GroupName="TestGroup">Option 1</RadioButton>
        <RadioButton GroupName="TestGroup">Option 2</RadioButton>
    </StackPanel>
</Window>

あなたが望むのは、です RadioButton それはaのように見えます ToggleButton, 、実際には、ToggleButtonのスタイルをそのタイプによる静的リソースと暗黙的に参照できます。

<RadioButton Style="{StaticResource {x:Type ToggleButton}}" />

RadiobuttonはToggleButtonの子孫であるため、これは機能しているようです。たとえば、使用することはできません {StaticResource {x:Type ComboBox}}.

使用するためにドキュメントを追跡することはできません x:Type のリソースとして Style;誰かがどこを見るべきか知っているなら、私はそれを見ることに興味があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top