通过 Tab 键切换到组合框 (MVVM) 时 SelectedValue 丢失

发布于 2024-12-06 10:07:09 字数 2927 浏览 1 评论 0原文

我有一个带有 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 技术交流群。

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

发布评论

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

评论(2

烟酉 2024-12-13 10:07:09

尝试使用 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.

蓝礼 2024-12-13 10:07:09

通过删除 EventTrigger EventName="SelectionChanged 部分修复了此问题。
触发器用于根据组合框 A 的选择生成组合框 B 的项目源。

我用事件处理程序替换了该功能

     _selectedScore.Model.PropertyChanged += SelectedScore_PropertyChanged;

    public void SelectedScore_PropertyChanged(object sender, PropertyChangedEventArgs  e)
    {
        if (e.PropertyName =="posid" )
        {
            this.UpdateFilteredRules(SelectedScore.Model.posid); 

        }
        if (e.PropertyName == "playerid")
        {
            this.SelectedScore.Model.posid = this.SelectedScore.PlayerVM.GetPosId(SelectedScore.Model.playerid).Model.posid;
        }
    }

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

     _selectedScore.Model.PropertyChanged += SelectedScore_PropertyChanged;

    public void SelectedScore_PropertyChanged(object sender, PropertyChangedEventArgs  e)
    {
        if (e.PropertyName =="posid" )
        {
            this.UpdateFilteredRules(SelectedScore.Model.posid); 

        }
        if (e.PropertyName == "playerid")
        {
            this.SelectedScore.Model.posid = this.SelectedScore.PlayerVM.GetPosId(SelectedScore.Model.playerid).Model.posid;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文