2010-10-13 13 views
7

HI, WPF'de çalışıyorum ve ilginç bir gereksinim var. Üç onay için onay kutularına ihtiyacım var, bu nedenle yalnızca alt öğelerden bazıları seçildiğinde belirsiz olarak gösterilir. Ancak bir kullanım tıklandığında, doğru veya yanlış seçilmesini isterim. Belirsiz duruma geçmek için üçlü onay kutusunu zorlama

item - indeterminate 
    subItem - checked 
    subItem - unchecked 
    subItem - checked 

kullanıcı item tıkladığında

, onay kutusu işaretli ve işaretsiz arasında dönüşümlü olmalıdır: İşte

benim gereksinimlerini temsil eden bir hikayedir. Kullanıcı asla 'belirsiz' bir durum seçmemelidir. Mümkün mü?

cevap

17

XAML:

<CheckBox IsThreeState="True" IsChecked="{x:Null}" Click="CheckBox_Clicked" /> 

kod arkadaki:

private void CheckBox_Clicked(object sender, RoutedEventArgs e) 
    { 
    var cb = e.Source as CheckBox; 
    if (!cb.IsChecked.HasValue) 
     cb.IsChecked = false; 
    } 

için çözeltide gibi arka planda kodlama çözümü, o zaman olabilir alt sınıf kendi kontrolünü beğenmezseniz this soru.

+0

Bingo! teşekkürler dostum. Belirsiz olay ile çalıştı ve işe yaramadı, ama tıkladı ... garip :) – TerrorAustralis

12

CheckBox'ınızla Bağlama kullanırsanız daha kolaydır.

XAML:

<Window x:Class="WpfApplication39Checkbox.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <CheckBox Content="CheckBox" HorizontalAlignment="Left" Margin="128,95,0,0" VerticalAlignment="Top" IsThreeState="False" IsChecked="{Binding CheckState}"/> 
     <Button Content="Button" HorizontalAlignment="Left" Margin="46,241,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> 
     <Button Content="Button" HorizontalAlignment="Left" Margin="139,241,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/> 
     <Button Content="Button" HorizontalAlignment="Left" Margin="235,241,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/> 
    </Grid> 
</Window> 

kod arkadaki:

using System.ComponentModel; 
using System.Runtime.CompilerServices; 
using System.Windows; 

namespace WpfApplication39Checkbox 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window, INotifyPropertyChanged 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      DataContext = this; 
     } 

     private bool? checkState; 

     public bool? CheckState 
     { 
      get { return checkState; } 
      set 
      { 
       checkState = value; 
       OnPropertyChanged("CheckState"); 
      } 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      CheckState = false; 
     } 

     private void Button_Click_1(object sender, RoutedEventArgs e) 
     { 
      CheckState = true; 
     } 

     private void Button_Click_2(object sender, RoutedEventArgs e) 
     { 
      CheckState = null; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

Sen noktaya bakın? CheckBox'ı IsThreeState = "False" olarak ayarlayın, ancak CodeBehind üçüncü durumunu ve CheckBox beklendiği gibi davranır.

İlgili konular