开发者

SelectedValue lost when tabbing to a combobox (MVVM)

开发者 https://www.devze.com 2023-04-07 15:04 出处:网络
I have a datagridtemplatecolumn with CellTemplate / CellEditingTemplate, works ok, after loading it shows the previously choosen selectedvalue bound from the model.

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">
 开发者_Go百科                                       <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


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.


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;
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号