-1

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

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

也是一个文本框。

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

一旦 ComboBox SelectedValue 更改为 B,我无法弄清楚如何更新 TextBox isEnabled 属性。一旦我退出并返回视图,它就可以工作,但我希望它是即时的。

谢谢。

4

3 回答 3

3

I have done a sample for you to understand it. Since it's more of a view based thing, I suggest using a converter and include the code in the view and converter itself. See the code below. I tested it and it is working fine at my end.

When you select B the textbox is enabled, when selection changes to any other, the textbox is disabled.

XAML Code :

<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 Code :

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();
        }
    }
}
于 2016-07-06T05:58:24.040 回答
0

过去我在绑定到SelectedValue并正确引发事件时遇到了一些问题。除非你有明确的理由,否则我喜欢绑定到SelectedItem

我在此操作中发现的问题是,与对象的绑定不一定bool会通过更改SelectedItemComboBox

如果您希望将两者链接起来,一种简单的方法是在bool isTypeB属性的设置器中为属性引发 Property changed 事件"Type"

(因为我不知道“类型”的类型,所以我假设它是一个字符串):

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));
    }
于 2016-07-05T20:57:55.637 回答
0

如果您的 Binding 第一次有效而后来无效,则您有关更改属性的通知可能无效。因此,请确保您绑定到 DependencyProperty 的属性,或者在您的 setter 中引发 PropertyChanged 事件(INotifyPropertyChanged 接口的成员)。

于 2016-07-05T16:26:57.510 回答