[英]textbox isReadonly to change with combobox selecteditem
我有datatemplate和5行的列表。 因此,在每一行中都有一個帶有兩個組合框項目“是”和“否”的組合框。 因此,在加載窗口時,ListBox行中的文本框在Datatemplate中設置為readonly =“ True”。 但是,當我從單獨行的組合框項目中選擇“否”時,文本框應變為可編輯狀態,並且列表中的每個單獨行均為isReadonly =“ False”。 我的ListBox項是5。如何執行此操作?
//xaml
<ListBox x:Name="wbListDataTemplate"
ItemsSource="{Binding wbVisibleItems}"
DataContext="{DynamicResource wbItem}"
Background="{x:Null}"
SelectedItem="{Binding wbSelectedItem, Mode=TwoWay, UpdateSourceTrigger=Default}"
IsSynchronizedWithCurrentItem="True" Canvas.Top="33" Height="152" Width="628" LostFocus="wbListDataTemplate_LostFocus" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Initialized="wbListDataTemplate_Initialized_1">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Grid.ColumnSpan="1" Grid.RowSpan="1" Height="39" Width="642" Margin="0,0,0,-14" >
<Grid x:Name="Grid1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="697" Margin="10,0,0,0" Height="54" >
<Label Margin="0,3,0,5" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2"/>
<ComboBox x:Name="wbselect" Margin="0,0,60,1" Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Loaded="wbselect_Loaded" >
<ComboBoxItem x:Name="wbyes" IsSelected="True" Content="yes"></ComboBoxItem>
<ComboBoxItem x:Name="wbno" Content="no"></ComboBoxItem>
</ComboBox>
<TextBox x:Name="wbdepth" Text="" MaxLength="20" Margin="217,0,230,1" LostKeyboardFocus="wbdepth_LostKeyboardFocus" Grid.ColumnSpan="2" IsReadOnly="True"/>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
盡管我建議不要為此目的使用ComboBox(並且更喜歡使用CheckBox或ToggleButton),但是您可以在TextBox樣式的ComboBox的SelectedIndex
屬性上使用DataTrigger:
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ComboBox x:Name="cb" SelectedIndex="0">
<ComboBoxItem>Yes</ComboBoxItem>
<ComboBoxItem>No</ComboBoxItem>
</ComboBox>
<TextBox Grid.Column="1">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedIndex, ElementName=cb}"
Value="0">
<Setter Property="IsReadOnly" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
</DataTemplate>
我將使用一個看起來像這樣的IValueConverter
。
public class ReadonlyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var content = ((ComboBoxItem)value).Content;
var isEnabled = content.Equals("yes");
return isEnabled;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
而在您的xaml中,您必須進行更改
IsReadOnly="True"
至
IsReadOnly="{Binding ElementName=wbselect, Path=SelectedItem, Converter={StaticResource ReadOnlyConverter}}"
而且您必須為轉換器添加參考
<Window xmlns:converter="clr-namespace:WpfApplication1.Converters">
<Window.Resources>
<ResourceDictionary>
<converter:ReadonlyConverter x:Key="ReadOnlyConverter"/>
</ResourceDictionary>
</Window.Resources>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.