【问题标题】:textbox isReadonly to change with combobox selecteditem文本框 isReadonly 与组合框选定项一起更改
【发布时间】:2017-07-05 08:59:46
【问题描述】:

我有数据模板和 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>

【问题讨论】:

  • 直 = 是,锥形 = 否?
  • @MightyBadaboom 请立即检查编辑
  • 看看我的回答

标签: c# wpf xaml combobox textbox


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      相关资源
      最近更新 更多