简体   繁体   English

将文本框绑定到组合框显示成员

[英]Binding a textbox to a combobox display member

I have a combobox with the SelectedItem property bound to a DataRowView and the ItemSource to a DataView. 我有一个组合框,其中SelectedItem属性绑定到DataRowView,而ItemSource绑定到DataView。

The binding is like this: 绑定是这样的:

   <ComboBox Grid.Row="1" Grid.Column="1" KeyboardNavigation.TabIndex="0" Width="300"
              ItemsSource="{Binding Path=MainConfigItems, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
            SelectedValuePath="Id" 
              DisplayMemberPath="Name"
              SelectedItem="{Binding MainConfigSelectedItem}"
              Name="cmbBoxMainConfig"
             VerticalAlignment="Center" HorizontalAlignment="Left" IsEnabled="{Binding IsEnabledMainConfig}">
  </ComboBox>

Now, based on a condition, I need to hide the combobox and display a textbox in its place with text as the display member of the combobox. 现在,根据条件,我需要隐藏组合框并在其位置显示一个文本框,并以文本作为组合框的显示成员。 In this case, the 'Name' attribute of the DataRowView. 在这种情况下,为DataRowView的“名称”属性。

What would be the best way to do this? 最好的方法是什么?

<TextBox Text="{Binding MainConfigSelectedItem.Name}"/>

if MainConfigSelectedItem is a DataRowView you need to use the indexer in your binding ( MainConfigSelectedItem[Name]). 如果MainConfigSelectedItem是DataRowView,则需要在绑定中使用索引器(MainConfigSelectedItem [Name])。

i would use style trigger to change visibility. 我会使用样式触发器来更改可见性。

put a datatrigger on your combo and textbox: 将datatrigger放在您的组合和文本框中:

<ComboBox.Style>
   <Style TargetType="ComboBox">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsEnabledMainConfig, UpdateSourceTrigger=PropertyChanged}" Value="false" >
        <Setter Property="Visibility" Value="Hidden"/>
        </DataTrigger>
      </Style.Triggers>
  </Style>
</ComboBox.Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM