2016-10-21 17 views
0

ラジオボタンをリストボックス内で使用していますが、無効にすることはできません。 検索後、私はIsEnabledとIsHitTestVisibleに関するリンクを発見しました。 IsEnabledをfalseに設定すると、ボタンとテキストはグレーに変わりますが、それでもクリック可能です。 IsHitTestVisibleを設定してもこれを変更することはありません。計算し、ブール値を返すC#でWPFラジオボタンIsHitTestVisibleが機能しない

<ListBox ItemsSource="{Binding Source}" BorderThickness="0" VerticalAlignment="Stretch" Background="Transparent" SelectedItem="{Binding SelectedSource, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" > 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <RadioButton IsHitTestVisible="{Binding IsEnabled}" IsEnabled="{Binding IsEnabled}" Margin="0 2 0 0" FontSize="12" FontFamily="Segoe UI Regular" Content="{Binding name}" GroupName="GroupList" > 
       <RadioButton.Style> 
        <Style TargetType="{x:Type RadioButton}"> 
         <Setter Property="IsChecked" Value="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" /> 
        </Style> 
       </RadioButton.Style> 
      </RadioButton> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

プロパティ:私のXAMLcodeはこのようになります。

public bool IsEnabled 
{  
    get 
    { 
     IsEnabledCalculate(); 
     return isEnabled; 
    } 
} 

選択を無効にする方法についてのご意見はありますか?

+0

はあなたですINotifyPropertyChangeの実装を?このプロパティは、UIなしで変更があることをUIに通知しません。 – adminSoftDK

答えて

1

IsHitTestVisibleRadioButtonに、ListBoxを使用すると良いスタートです。このようにしてRadioButtonをクリックすると、イベントはそれを「通過」し、ListBoxItemによって処理されます(アイテムが選択されます)。その後IsSelectedというプロパティーをバインドすることによりIsCheckedプロパティを操作することができます。ListBoxItemのプロパティ。

これでも、あなたが望む目標を達成するにはまだ十分ではありません。まず、IsEnabledプロパティを間違ったレベル、つまりRadioButtonにバインドしています。あなたが気付いたように、これでもListBoxItemが選択されます。だから、あなたの代わりにListBoxItmIsEnabledプロパティにごIsEnabledプロパティをバインドする必要があります。

 <ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="IsEnabled" Value="{Binding Path=IsEnabled}"/> 
      </Style> 
     </ListBox.ItemContainerStyle> 

はまだ1つのマイナーな問題がここにあります。あなたのIsEnabledプロパティがコードビハインドで変更された場合、ListBoxItemは無効になりますが、RadioButtonはまだチェックされます。これを防ぐため、アイテムがあなたがDataTrigger使用することができます無効になりますたびRadioButtonをオフにするには:

  <DataTemplate> 
       <RadioButton GroupName="GroupList" IsHitTestVisible="False" 
        Margin="0 2 0 0" FontSize="12" FontFamily="Segoe UI Regular" 
        Content="{Binding name}" > 
        <RadioButton.Style> 
         <Style TargetType="{x:Type RadioButton}"> 
          <Setter Property="IsChecked" Value="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" /> 
          <Style.Triggers> 
           <DataTrigger Binding="{Binding IsEnabled}" Value="False"> 
            <Setter Property="IsChecked" Value="False"/> 
           </DataTrigger> 
          </Style.Triggers> 
         </Style> 
        </RadioButton.Style> 
       </RadioButton> 
      </DataTemplate> 
+0

ありがとうございました!今度は、propertychangedの値を一度に更新することができないという問題があります。 – SEASN

+0

@SEASNどのプロパティを意味していますか? – Arie

関連する問題