我做了一個樣本,爲您瞭解它。 由於它更多的是基於視圖的東西,我建議使用轉換器並將代碼包含在視圖和轉換器中。請參閱下面的代碼。我測試了它,它在我的最後工作正常。
當您選擇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();
}
}
}
什麼是 「isTypeB」?你有沒有誤認爲[另一個網站](http://www.psychicfriendsnetwork.com)? –