WPF数据绑定链接文本块文本到组合框选择 [英] WPF Databinding Link Textblock text to Combobox Selection

查看:72
本文介绍了WPF数据绑定链接文本块文本到组合框选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据网格中有这个xaml:

I have this xaml in my datagrid:

                <DataGridTemplateColumn Header="Status" Width="*" IsReadOnly="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock x:Name="StatusText" Text="{Binding Description}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <ComboBox 
                            ItemsSource="{Binding Source={StaticResource StatusItems}}"
                            SelectedValue="{Binding Status, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}"
                            DisplayMemberPath="Description"
                            SelectedValuePath="Status"
                            x:Name="Combo"
                            />
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

当我更改组合框的值时,数据集会完美更新,但文本块文本不会更新更新到新值我必须为文本块重新填充整个数据集,以匹配新选择的组合框值。我看到正确的方法是实现INotifyPropertyChanged,但这至少要从我了解类似文章的理解出发,需要对应用程序填充数据集的方式进行重大更改。我没有可以实现的模型,我想知道是否可以简单地在文本块上设置触发器,以在组合框选择更改时更改值。

When i change the value of the combobox, the dataset updates perfectly, but the textblock text doesn't update to the new value I have to refill the entire dataset for the textblock to match the newly selected combobox value. I see the correct method is to implement INotifyPropertyChanged, but that would require significant changes to the way the app fills the dataset, at least from what i understand reading similar posts. I don't have a model that i can implement on, i'm wondering if i can simply set a trigger on the textblock that will change the value whenever the combobox selection changes.

这是我填充数据网格的方式,如果有人知道我可以如何修改它以实现INotifyPropertyChanged,那也很好,但是我认为如果没有定义模型,那将是行不通的(再说一次,只是继续我看到别人在做。)

Here is how i am filling the datagrid, if someone knows how i can modify this to implement INotifyPropertyChanged, that would also be great, but i don't think that will work without a model defined (again, just going on what i see others doing).

Dim con As New SqlConnection(str)
Dim ds As New DataSet()
Dim Adpt As New SqlDataAdapter
Adpt.SelectCommand = New SqlCommand(com, con)

    con.Open()

    Adpt.Fill(ds, "dbo.tmfCNCComponent_threed")
    dataGrid1.ItemsSource = ds.Tables("dbo.tmfCNCComponent_threed").DefaultView


    con.Close()


推荐答案

将此属性添加到ComboBox: IsSynchronizedWithCurrentItem = False 。默认情况下, CollectionViewSource 在选择器(例如ComboBox,ListBox等)中跟踪选定的项目。如果对多个控件使用相同的 CollectionViewSource ,除非您明确禁止,否则它将对所有控件施加相同的选择。如果您将同一集合与多个选择器一起使用,则在某些情况下,您希望它们全部同步选定的项目。这不是其中一种情况。

Add this attribute to the ComboBox: IsSynchronizedWithCurrentItem="False". By default, CollectionViewSource tracks the selected item in a selector (e.g. ComboBox, ListBox, etc.). If you use the same CollectionViewSource for multiple controls, it will impose the same selection on all of them unless you explicitly prevent that. If you're using the same collection with multiple selectors, there are cases where you want them all to synchronize the selected item. This is not one of those cases.

您需要一个只读的CellTemplate和一个可编辑的CellEditingTemplate。我们可以为这两个模板使用相同的模板,并在不编辑单元格时将其禁用的组合框。

You need a read-only CellTemplate and an editable CellEditingTemplate. We can use the same template for both, with a ComboBox that's disabled when the cell isn't being edited.

结果:

<DataGrid x:Name="DataGrid" AutoGenerateColumns="False" CellEditEnding="DataGrid_CellEditEnding">
    <DataGrid.Resources>
        <DataTemplate x:Key="StatusColumnTemplate">
            <ComboBox 
                ItemsSource="{Binding Source={StaticResource StatusItems}}"
                SelectedValue="{Binding Status, UpdateSourceTrigger=PropertyChanged}"
                DisplayMemberPath="Description"
                SelectedValuePath="Status"
                IsSynchronizedWithCurrentItem="False"
                IsEnabled="{Binding IsEditing, RelativeSource={RelativeSource AncestorType=DataGridCell}}"
                />
        </DataTemplate>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTemplateColumn 
            Header="Status Shared"
            CellTemplate="{StaticResource StatusColumnTemplate}"
            CellEditingTemplate="{StaticResource StatusColumnTemplate}"
            />

您现在所拥有的显然是行不通的,因为网格行不能 有一个说明列。

What you've got now clearly can't work because the grid rows don't have a Description column.

NotifyOnSourceUpdated = True 与此处发生的一切无关。

NotifyOnSourceUpdated=True has nothing to do with anything that's happening here.

这篇关于WPF数据绑定链接文本块文本到组合框选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆