2016-07-05 51 views
-1

所以我有一个组合框绑定到项目列表。 {A,B,C,D,E}TextBox isEnabled绑定依赖于组合框项目

<ComboBox x:Name="cmbDMS" ItemsSource="{Binding Types}" SelectedValue="{Binding Type,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="top" Width="120" SelectionChanged="cmbDMS_SelectionChanged" /> 

也是一个TextBox。

<TextBox IsEnabled="{Binding isTypeB}" Grid.Row="6" Width="250" Margin="0,2" /> 

我无法弄清楚如何,一旦组合框的SelectedValue已更改为B.它的工作原理,一旦我离开,回来到视图,但我希望它是即时更新的文本框IsEnabled属性。

谢谢。

+0

什么是 “isTypeB”?你有没有误认为[另一个网站](http://www.psychicfriendsnetwork.com)? –

回答

1

我过去遇到过一些问题,需要绑定到SelectedValue,并且正确地引发事件。除非你有一个明确的原因,我喜欢绑定到SelectedItem

我有这个操作中发现的问题,是对bool对象的约束力并不一定会获得通过ComboBox

SelectedItem的变化更新

如果你想被链接的两个,一个简单的方法做,这是对属性格式的二传手内提高属性更改事件为bool isTypeB属性:

(因为我不知道的类型“类型”我会认为它是一个字符串):

public string Type 
{ 
    get{...} 
    set 
    { 
    //...set logic 
    RaisePropertyChanged(); //will notify when "Type" changes 
    RaisePropertyChanged("isTypeB"); //will notify that isTypeB is changed 
    } 
} 
... 
public bool isTypeB => Type == "TypeB"; 

参考了RaisePropertyChanged

public event PropertyChangedEventHandler PropertyChanged; 

    public virtual void RaisePropertyChanged([CallerMemberName] string propertyName = "") 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
0

如果您的绑定是第一次,后来它不会,那么您关于更改属性的通知可能不起作用。所以确保你绑定到属性是DependencyProperty,或者在你的setter中引发PropertyChanged事件(INotifyPropertyChanged接口的成员)。

2

我做了一个样本,为您了解它。 由于它更多的是基于视图的东西,我建议使用转换器并将代码包含在视图和转换器中。请参阅下面的代码。我测试了它,它在我的最后工作正常。

当您选择B时,文本框处于启用状态,当选择更改为任何其他文本框时,文本框将被禁用。

XAML代码:

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:WpfApplication1" 
    xmlns:viewModel="clr-namespace:WpfApplication1.ViewModel" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525" > 
    <Window.Resources> 
     <viewModel:TestConverter x:Key="TestConverter" /> 
    </Window.Resources> 
    <Grid Margin="329,0,0,0"> 
     <ComboBox x:Name="cmbDMS" HorizontalAlignment="Left" VerticalAlignment="top" Width="120" > 
      <ComboBoxItem Content="A"/> 
      <ComboBoxItem Content="B"/> 
      <ComboBoxItem Content="C"/> 
      <ComboBoxItem Content="D"/> 
      <ComboBoxItem Content="E"/> 
     </ComboBox> 
     <TextBox Grid.Row="6" Height="60" Width="250" Margin="0,2" IsEnabled="{Binding ElementName=cmbDMS, Path=Text, Converter={StaticResource TestConverter}}" /> 
    </Grid> 
</Window> 

TestConverter.cs代码:

using System; 
using System.Globalization; 
using System.Windows.Data; 

namespace WpfApplication1.ViewModel 
{ 
    public class TestConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      if (value != null) 
      { 
       var si = value.ToString(); 
       if (si.Equals("B")) 
        return true; 
      } 
      return false; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
}