选择更改时 DataGridComboBoxColumn 会丢失其内容

发布于 2024-09-06 08:16:07 字数 1793 浏览 2 评论 0原文

当我单击 DataGridComboBoxColumn 中的单元格时,组合框变得可见,我可以选择项目。当我选择一个项目时,它在顶部可见,这很好。但是,当单元格(又名 ComboBox)因为我在 DataGrid 中单击不同的内容而失去焦点时,那么我之前选择的单元格中就不再有可见的项目/文本。

如何保留该选择/选定的文本?

这就是我的代码:

<DataGridComboBoxColumn
           Width="*"
           Header="Monday"
           DisplayMemberPath="SchoolclassName"
           SelectedValueBinding="{Binding SchoolclassCodeMonday}"  
           ItemsSource="{Binding Source={StaticResource ClassCodes}}">

    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
            <Setter Property="ItemsSource" 
                    Value="{Binding Source={StaticResource ClassCodes}}" />
        </Style>
    </DataGridComboBoxColumn.ElementStyle>

    <DataGridComboBoxColumn.EditingElementStyle>                   
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" 
                    Value="{Binding Source={StaticResource ClassCodes}}" />
            <Setter Property="IsDropDownOpen" Value="True" />
        </Style>                   
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

似乎有一个解决我的问题的方法:http://wpf.codeplex.com/Thread/View.aspx?ThreadId=46627 (滚动到底部)但我无法将解决方案转移到我的问题。因为我的模型设置完全不同。

SchoolclassName 是 Schoolclass.cs 中的字符串属性 SchoolclassCodeMonday 是 TimeTable.cs 中的字符串属性 ClassCodes 又名 SchoolclassCodes 是 ObservableCollection|Schoolclass| 类型的属性

有人知道如何修复我的绑定吗?

When I click a cell in my DataGridComboBoxColumn the ComboBox gets visible and I can select items. When I have selected an item its visible at the top thats fine. But when the cell aka ComboBox loses its focus because I click something different in the DataGrid then there is no item/text visible anymore in the cell I have previously selected.

How can I keep that selection/selected text?

thats my code:

<DataGridComboBoxColumn
           Width="*"
           Header="Monday"
           DisplayMemberPath="SchoolclassName"
           SelectedValueBinding="{Binding SchoolclassCodeMonday}"  
           ItemsSource="{Binding Source={StaticResource ClassCodes}}">

    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
            <Setter Property="ItemsSource" 
                    Value="{Binding Source={StaticResource ClassCodes}}" />
        </Style>
    </DataGridComboBoxColumn.ElementStyle>

    <DataGridComboBoxColumn.EditingElementStyle>                   
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" 
                    Value="{Binding Source={StaticResource ClassCodes}}" />
            <Setter Property="IsDropDownOpen" Value="True" />
        </Style>                   
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

there seems to be a solution for my problem: http://wpf.codeplex.com/Thread/View.aspx?ThreadId=46627 (scroll to the bottom) but I can not transfer the solution to my problem. Because my model setup is quite different.

SchoolclassName is a string property in Schoolclass.cs
SchoolclassCodeMonday is a string property in TimeTable.cs
ClassCodes aka SchoolclassCodes is a property of type ObservableCollection|Schoolclass|

Someone knows how to fix my binding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倚栏听风 2024-09-13 08:16:07

我知道它可能不再需要了,但也许它会帮助其他人。
您的 ComboBox 在更改时不需要更新绑定吗?例如

SelectedValueBinding="{Binding SchoolclassCodeMonday}"

SelectedValueBinding="{Binding SchoolclassCodeMonday, 
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  

还要确保当可观察集合上的代码更改属性时,您会触发通知。

I know its probably not needed anymore but maybe it will help someone else.
Would your ComboBox not need to update the binding when it's changed? e.g.

SelectedValueBinding="{Binding SchoolclassCodeMonday}"

would be:

SelectedValueBinding="{Binding SchoolclassCodeMonday, 
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  

Also make sure you are firing a notification when the property is changed from code on your observable collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文