textbox isReadonly用combobox selecteditem改变

时间:2017-07-05 08:59:47

标签: c# wpf xaml combobox textbox

我有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>

2 个答案:

答案 0 :(得分:0)

虽然我建议不要为此目的使用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>

答案 1 :(得分:0)

我会使用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>