【问题标题】:WPF Listview - binding a text column to a combobox column selected itemWPF Listview - 将文本列绑定到组合框列选定项
【发布时间】:2011-05-11 07:48:00
【问题描述】:

我有一个列表视图:

<ListView ItemsSource="{Binding FieldMap.SourceTargetFieldMap, Mode=Default}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Source Fields" CellTemplate="{StaticResource sourceFieldsTemplate}"/>
            <GridViewColumn Header="Source Values" CellTemplate="{StaticResource =sourceValueTemplate}">
            <GridViewColumn Header="Target Field" CellTemplate="{StaticResource targetFieldTemplate}"/>
        </GridView>
    </ListView.View>
</ListView>

我也有这些绑定到的单元格数据模板:

<DataTemplate x:Key="sourceFieldsTemplate">
    <ComboBox x:Name="cbSourceField" SelectedValue="{Binding Path=SourceField, Mode=TwoWay}" DisplayMemberPath="FieldName" ItemsSource="{Binding SourceFieldValues}" Width="120" />
</DataTemplate>

<DataTemplate x:Key="sourceValueTemplate">
    <TextBox x:Name="tbSourceValue" Margin="5,0,0,0" DataContext="{Binding ElementName=cbSourceField, Path=SelectedItem}" Text="{Binding Path=FieldValue, Mode=TwoWay}" TextWrapping="Wrap"  Width="115" />
</DataTemplate>

<DataTemplate x:Key="targetFieldTemplate">
    <TextBox x:Name="tbTargetField" Margin="5,0,0,0" Text="{Binding Path=TargetField}" TextWrapping="Wrap" Width="155" IsReadOnly="True"/>
</DataTemplate>

我想要做的是,将 Source Value 文本框字段绑定到 SourceFields 组合框的选定项。因此,当从源字段组合框中选择一个项目时,来自 selectedItem 的属性(实际上是一个具有两个属性的对象 - 类似于字段名和值)填充文本框。

这两个控件都“隐藏”在数据模板中,我不确定如何填充另一个控件??

有什么想法吗?非常感谢!

马特

【问题讨论】:

    标签: wpf listview binding combobox wpf-4.0


    【解决方案1】:

    您可以在内容绑定到列表视图的选定项目的地方插入一个 ContentConteol - 然后日期模板应该负责显示 - 我会在文本框上设置只读,因为更改文本将无效。

    【讨论】:

    • 啊——我想我明白你的问题了。我会在您的数据对象上添加一个 SelectedItem 属性,让它实现 INotifyPropertyChanged 并让两个控件绑定到您的数据项上的 SelectedItem - 这样您的选择就远离 ui 并且数据模板没有问题。
    猜你喜欢
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    相关资源
    最近更新 更多