2015-06-14 83 views
2

我有一个checkbox绑定到一个对象的属性“IsValidCustomer”,我有一个listview持有一些客户。 每当我的用户在列表中选择任何Customer,我希望CheckboxChecked属性设置为False,这意味着我的“IsValidCustomer”属性也将自动设置为False。有没有使用WPF绑定来实现这一点的方法?绑定到属性和元素值 - WPF绑定

在这方面的任何帮助将高度appriciated。

问候

-Srikanth

回答

0
  • 首先确保你的观点的Datacontext设置为viewmodel实现了INotifyPropertyChangedinterface然后添加一个SelectedCustomer属性,将举行从ListView选择Customer

  • 每次设置SelectedCustomer时,检查它的价值,并设置IsValidCustomer属性 这里的全码:

视图模型

public class Customer 
{ 
    public String Name { get; set; } 
    public String Id { get; set; } 
} 
public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    private Customer _selectedCustomer; 
    public Customer SelectedCustomer 
    { 
     get 
     { 
      return _selectedCustomer; 
     } 

     set 
     { 
      if (_selectedCustomer == value) 
      { 
       return; 
      } 

      _selectedCustomer = value; 
      OnPropertyChanged(); 
      IsValidCustomer = (_selectedCustomer == null); 

     } 
    } 
    private ObservableCollection<Customer> _listCustomers; 
    public ObservableCollection<Customer> ListCustomers 
    { 
     get 
     { 
      return _listCustomers; 
     } 

     set 
     { 
      if (_listCustomers == value) 
      { 
       return; 
      } 

      _listCustomers = value; 
      OnPropertyChanged(); 
     } 
    } 
    private bool _isValidCustomer = false; 
    public bool IsValidCustomer 
    { 
     get 
     { 
      return _isValidCustomer; 
     } 

     set 
     { 
      if (_isValidCustomer == value) 
      { 
       return; 
      } 

      _isValidCustomer = value; 
      OnPropertyChanged(); 
     } 
    } 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

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

和视图

<StackPanel> 
    <CheckBox Content="IsValidCustomer" IsChecked="{Binding IsValidCustomer,Mode=TwoWay}"></CheckBox> 
    <ListView ItemsSource="{Binding ListCustomers}" SelectedItem="{Binding SelectedCustomer,Mode=TwoWay}"></ListView>   
</StackPanel> 
0

我相信你有这样的事情在您的型号:

 private bool _IsValidCustomer; 

    public bool IsValidCustomer 
    { 
     get { return _IsValidCustomer; } 
     set 
     { 
      _IsValidCustomer= value; 
      PropertyChanged(this, new PropertyChangedEventArgs("IsValidCustomer")); 
     } 
    } 

设置该布尔属性的绑定。

  <Style TargetType="ListViewItem"> 
       <Setter Property="IsSelected" Value="{Binding IsValidCustomer, Converter={StaticResource InverseBooleanConverter}}"/> 
      </Style> 

你的复选框将被绑定到这个也:

<CheckBox IsChecked="{Binding IsValidCustomer, Mode=TwoWay}"/> 

所以,我假设你开始与IsValidCustomer设置为true。在选择每一行时,您希望将其设置为false。

您将需要此相反的布尔转换器:

public class InverseBooleanConverter: IValueConverter 
{ 
    #region IValueConverter Members 

    public object Convert(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     if (targetType != typeof(bool)) 
      throw new InvalidOperationException("The target must be a boolean"); 

     return !(bool)value; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 

    #endregion 
} 
+0

嗨奥拉鲁..看来,这是为我工作.. excpt,当我选择复选框或任何一个列表项是选择所有列表项..不知道为什么它..只有改变我所做的是IsValidCustomer属性是在窗口级别datacontext和列表数据源是客户的列表,所以,我用FindAncetor来获取IsValidCustomer ..任何想法为什么我所有的列表视图项目选择..也,我的listview的视图并不是真正的listviewitems,而是一个Grid在Listview.view中绑定..感谢您的帮助,至此.. –

+0

@SrikanthAlluri IsValidCustomer应驻留在模型中。这样,每一行都会有自己的状态。我认为你必须区分每个客户。虽然一个是无效的,其他的可能会是这样,等等。在Window的上下文中设置,所有的绑定都会引用相同的属性,我相信这不是你想要的行为。 –

+0

@SrikanthAlluri还有一件事,也为Customer类实现INotifyPropertyChanged,并在每个属性的设置分支上添加PropertyChanged事件。 –