2012-09-26 12 views
7

WPFには、いくつかのテキストボックスとラジオボタンのコレクションがあります。私は任意のテキストボックスがフォーカスを取得する場合、名前を持つoly 1つのラジオボタンのIsCheckedプロパティを設定するトリガを使用したいと思います。私はいくつかの例をチェックしますが、私が探しているものを見つけることができませんでした。 私たちはMVVMパターンを使用しており、コードはありません。ターゲット名を指定したトリガー

私は、次のコードを試してみましたが、このコンパイルエラーを持っている:

TargetNameはプロパティには、私は他の記事を読み、DataTriggerは、問題を解決するスタイルセッター

<UserControl.Resources> 
      <Style x:Name="myTest" TargetType="TextBox"> 
       <Style.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="RadioButton.IsChecked" Value="True" TargetName="myRadioButton"></Setter> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </UserControl.Resources> 

に設定することはできません。

<Style x:Name="myTest2" TargetType="RadioButton" > 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding IsFocused, ElementName=myTextBox}" Value="True"> 
      <Setter Property="IsChecked" Value="True" ></Setter> 
     </DataTrigger>  
    </Style.Triggers> 
</Style> 
+1

あなたは私たちに、少なくともXAMLコードを示すことができ、私それが役立つだろう;) –

+0

MSDN 'あなたはどこセッターのコレクション(コレクションの範囲内の任意の要素の名前にこのプロパティを設定することができますから、このセッターの一部)が適用されます。これは、通常、このセッターを含むテンプレート内の名前付き要素です。 ' TargetNameは、主にコントロールテンプレート内で使用されています。単純に使用しようとしているようなスタイルではありません。 –

+2

WPFトリガー、スタイル、およびテンプレートの交差点で常にこの種の問題が発生している人はいますか?スタイル設定ツールでTargetNameを使用することはできません。トリガーコレクションには、スタイルに含まれていない限り、EventTriggersしか含めることができません。コントロールはデータテンプレートではなく、コントロールテンプレートでのみ動作します... – user1454265

答えて

1

私はあなたがGotFocusイベントを探していると思います。 XAMLで

:テキストボックスのいずれかが、それはmyRadioButtonという名前のRadioButtonをチェックします焦点を当ててしまった場合

<TextBox x:Name="textBox1" GotFocus="tb_GotFocus"/> 
<TextBox x:Name="textBox2" GotFocus="tb_GotFocus"/> 
<TextBox x:Name="textBox3" GotFocus="tb_GotFocus"/> 
<RadioButton x:Name="myRadioButton"/> 

は、その後、あなたのC#でイベントハンドラは、この

private void tb_GotFocus(object sender, RoutedEventArgs e) 
{ 
    myRadioButton.IsChecked = true; 
} 

のようになります。 MSDNから

+2

まあ、私はMVVMと背後にないコードを訴えていることを忘れてしまいます。 – AustinTX

1

You can set this property to the name of any element within the scope of where the setter collection (the collection that this setter is part of) is applied. This is typically a named element that is within the template that contains this setter.

TargetNameは、主にコントロールテンプレート内で使用していないだけで、あなたがそれを使用しようとしているようなスタイル内れます。あなたができることは、RadioButtonIsChecked DPをIsMouseOver DPのTextBoxにバインドすることです。

1

はのControlTemplateを作成し、ControlTemplate.Triggers

<ControlTemplate.Triggers> 
    <Trigger Property="HasText" Value="True"> 
     <Setter Property="Visibility" TargetName="LabelText" Value="Hidden" /> 
    </Trigger> 
</ControlTemplate.Triggers> 
0

各RadioButtonの要素をトリガとして、それぞれのテキストボックスを提供するための書き込みスタイルにあなたのトリガーを追加します。以下は、3つのテキストボックスの例です。& 3つのラジオボタンです。

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <TextBox x:Name="txtBox0" Grid.Row="0"/> 
    <TextBox x:Name="txtBox1" Grid.Row="1"/> 
    <TextBox x:Name="txtBox2" Grid.Row="2"/> 

    <StackPanel Grid.Row="3" Orientation="Horizontal"> 
     <RadioButton GroupName="grp1" Content="txt1"> 
      <RadioButton.Style> 
       <Style TargetType="RadioButton"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding IsFocused, ElementName=txtBox0}" Value="True"> 
          <Setter Property="IsChecked" Value="True"/> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </RadioButton.Style> 
     </RadioButton> 
     <RadioButton GroupName="grp1" Content="txt2"> 
      <RadioButton.Style> 
       <Style TargetType="RadioButton"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding IsFocused, ElementName=txtBox1}" Value="True"> 
          <Setter Property="IsChecked" Value="True"/> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </RadioButton.Style> 
     </RadioButton> 
     <RadioButton GroupName="grp1" Content="txt3"> 
      <RadioButton.Style> 
       <Style TargetType="RadioButton"> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding IsFocused, ElementName=txtBox2}" Value="True"> 
          <Setter Property="IsChecked" Value="True"/> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </RadioButton.Style> 
     </RadioButton> 
    </StackPanel> 
</Grid> 
関連する問題