繁体   English   中英

如何将ComboBox SelectedValue属性绑定到ListView WPF中的TextBox Text属性

[英]How to binding a ComboBox SelectedValue property to TextBox Text property inside ListView WPF

我在ListView中有一个ComboBox和TextBox,如何将SelectedValue绑定到Text,我的代码:

<GridViewColumn Width="130">
     <GridViewColumnHeader Content="Caracteristica" />
         <GridViewColumn.CellTemplate>
             <DataTemplate>
                 <Grid>
                     <ComboBox x:Name="cmbCaracteristica" Width="100" />
                 </Grid>
             </DataTemplate>
         </GridViewColumn.CellTemplate>
    </GridViewColumn>
   <GridViewColumn Width="100">
         <GridViewColumnHeader Content="Tipo" />
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <Grid>
                      <TextBox x:Name="txtTipoValor" Text="{Binding Path=SelectedValue, ElementName=cmbCaracteristica, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Left"/>
                     </Grid>
               </DataTemplate>
             </GridViewColumn.CellTemplate>
         </GridViewColumn>

这行不通。 PD:C#中ComboBox的ItemSource加载。

您尝试过DisplayMemberPath属性吗? 在相同情况下为我工作。

  <ListView DisplayMemberPath="MainList" </ListView>

适用于MainList中的选定项目,您可以对文本框执行相同的操作。

ComboBoxSelectedItem属性绑定到IEnumerable<T> ItemsSource类型T的属性,然后将TextBox绑定到相同的source属性:

<GridViewColumn Width="130">
    <GridViewColumnHeader Content="Caracteristica" />
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <ComboBox x:Name="cmbCaracteristica" Width="100"
                         SelectedItem="{Binding SelectedItem}" />
            </Grid>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="100">
    <GridViewColumnHeader Content="Tipo" />
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <TextBox x:Name="txtTipoValor"
                         Text="{Binding Path=SelectedItem, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Left"/>
            </Grid>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

确保类型T实现INotifyPropertyChanged接口。

你不能在一个元素绑定CellTemplate在另一个元素CellTemplate使用ElementName

暂无
暂无

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

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