通过 Tab 键切换到组合框 (MVVM) 时 SelectedValue 丢失
我有一个带有 CellTemplate / CellEditingTemplate 的 datagridtemplatecolumn,工作正常,加载后显示先前从模型中选择的选定值绑定。 但问题是,当我通过列“制表符”时,组合框会丢失它的选定值并给我一个空值?
我希望我的代码有问题:
<data:DataGridTemplateColumn x:Name="colPosId" Width="80">
<data:DataGridTemplateColumn.HeaderStyle>
<Style TargetType="dataprimitives:DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Resource.lblPosId, Source={StaticResource CustomLocStrings}}" Style="{StaticResource ColumnHeaderTextBoxStyleCentered}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</data:DataGridTemplateColumn.HeaderStyle>
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Model.posid}" Style="{StaticResource ColumnTextBoxStyleCentered}" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
<data:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox
Height="23" HorizontalAlignment="Left"
x:Name="cmbPositions" VerticalAlignment="Top" Width="100" ItemsSource="{Binding PositionVM.Positions, Mode=TwoWay}" SelectedValue="{Binding Model.posid, Mode=TwoWay}"
DisplayMemberPath="Model.name" SelectedValuePath="Model.posid">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command="{Binding MainScore.SelectionPosChangedCommand, Mode=OneWay, Source={StaticResource Locator}}" CommandParameter="{Binding SelectedValue, ElementName=cmbPositions}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</DataTemplate>
</data:DataGridTemplateColumn.CellEditingTemplate>
</data:DataGridTemplateColumn>
亲切的问候,
迈克
I have a datagridtemplatecolumn with CellTemplate / CellEditingTemplate, works ok, after loading it shows the previously choosen selectedvalue bound from the model.
But the problem is that when I 'tab' through the columns the combobox loses it's selectedvalue and gives me an empty one?
I hope there's something wrong with my code:
<data:DataGridTemplateColumn x:Name="colPosId" Width="80">
<data:DataGridTemplateColumn.HeaderStyle>
<Style TargetType="dataprimitives:DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Resource.lblPosId, Source={StaticResource CustomLocStrings}}" Style="{StaticResource ColumnHeaderTextBoxStyleCentered}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</data:DataGridTemplateColumn.HeaderStyle>
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Model.posid}" Style="{StaticResource ColumnTextBoxStyleCentered}" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
<data:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox
Height="23" HorizontalAlignment="Left"
x:Name="cmbPositions" VerticalAlignment="Top" Width="100" ItemsSource="{Binding PositionVM.Positions, Mode=TwoWay}" SelectedValue="{Binding Model.posid, Mode=TwoWay}"
DisplayMemberPath="Model.name" SelectedValuePath="Model.posid">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand Command="{Binding MainScore.SelectionPosChangedCommand, Mode=OneWay, Source={StaticResource Locator}}" CommandParameter="{Binding SelectedValue, ElementName=cmbPositions}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</DataTemplate>
</data:DataGridTemplateColumn.CellEditingTemplate>
</data:DataGridTemplateColumn>
Kind regards,
Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 SelectedItem 而不是 SelectedValue。
为什么使用 SelectionChangedTrigger?当您将 SelectedItem 与 TwoWay 绑定时,您将选择到视图模型。
您还应该将 ItemsSource Binding 更改为 Mode=OneWay 或 OneTime。双向绑定在这里没有意义。
try using SelectedItem instead of SelectedValue.
Why do you use a SelectionChangedTrigger? when you bind the SelectedItem with TwoWay you get the selection to your viewmodel.
you should also changed the ItemsSource Binding to Mode=OneWay or OneTime. TwoWay Binding makes no sense here.
通过删除 EventTrigger EventName="SelectionChanged 部分修复了此问题。
触发器用于根据组合框 A 的选择生成组合框 B 的项目源。
我用事件处理程序替换了该功能
Fixed it by removing the EventTrigger EventName="SelectionChanged part.
The trigger was for generating the itemsource for combobox B based on the selection of combobox A.
I replaced the functionallity with an eventhandler