在DataGrid中更改Combobox的选定值wpf

时间:2013-09-14 15:34:20

标签: c# wpf mvvm

逗人, 我在一个数据网格中有一个combobox列绑定到我的ViewModel中的可观察集合Markets,而DataGrid绑定到一个Collection ClientMarketCode,它是NewClient对象中的一个集合。 ClientMarketCode具有此属性Code,MarketCodeTypeID,MarketID。 所以我使用了一个绑定到市场的组合框,以防用户想要编辑市场它显示所有市场和用户选择他想要的市场。我在选择任何其他市场时遇到问题,因为所选行中的市场价值在选择后没有变化来自组合框的新市场。

e.g:

让我们说第一排有KSA市场,然后我点击美国后将它从组合框更改为美国它返回bac给KSA我不知道y。

这是我的观点模型:

public class MarketsViewModel : ObservableObject, IMarketsViewModel, INavigationAware, IConfirmNavigationRequest, IRegionMemberLifetime
{
    #region MarketsViewModel
    public MarketsViewModel()
    {
        this.GetMarkets();
    }
    public ObservableCollection<Market> Markets
    {
        get { return m_Market; }
        set
        {
            m_Market = value;
            RaisePropertyChanged("Markets");
        }
    }

    private void GetMarkets()
    {
        try
        {
            Market[] MarketArr;

            using (var client = new ClientServiceProxy())
            {
                MarketArr = client.GetAllMarkets();
            }
            if (MarketArr != null)
            {
                this.Markets = new ObservableCollection<Market>(MarketArr);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }
}

继承人XAML:

<Custom:C1DataGrid x:Name="c1DataGrid"  
                   AutoGenerateColumns="False"

                   Height="490"  ItemsSource="{Binding    Path=NewClient.ClientMarketCodes,Mode=TwoWay,UpdateSourceTrigger=LostFocus,ValidatesOnDataErrors=True,ValidatesOnExceptions=True}">
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FFBEBE00" Offset="0.5"/>
            <GradientStop Color="#FFEDED9A" Offset="0.496"/>
        </LinearGradientBrush>
    </Custom:C1DataGrid.HeaderBackground>
    <Custom:C1DataGrid.Columns>
        <!--<Custom:DataGridCheckBoxColumn  Header="Code"/>-->

        <Custom:DataGridTemplateColumn Header="Markets">
            <Custom:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding MarketCodeType.Market.NameA}"/>
                </DataTemplate>
            </Custom:DataGridTemplateColumn.CellTemplate>
            <Custom:DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <ComboBox Name="cmbMarkets" ItemsSource= "{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=my:MarketsView},Path=DataContext.Markets,Mode=TwoWay}" 
DisplayMemberPath="NameA" SelectedValue="{Binding Path=MarketCodeType.Market.MarketID,Mode=TwoWay}" 
        SelectedValuePath="MarketCodeType.MarketID"
SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=my:MarketsView},Path=DataContext.selectedMarket,Mode=TwoWay}"    />
                </DataTemplate>
            </Custom:DataGridTemplateColumn.CellEditingTemplate>
        </Custom:DataGridTemplateColumn> 


        </Custom:DataGridTemplateColumn>
    </Custom:C1DataGrid.Columns>

</Custom:C1DataGrid>

1 个答案:

答案 0 :(得分:2)

总结一下我的评论,你的装订应该是:

<DataTemplate>
    <ComboBox Name="cmbMarkets" DisplayMemberPath="NameA"
              ItemsSource= "{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=my:MarketsView},Path=DataContext.Markets}"
              SelectedItem="{Binding MarketCodeType.Market, Mode=TwoWay}"/>
</DataTemplate>

或(对此不太确定):

<DataTemplate>
    <ComboBox Name="cmbMarkets" DisplayMemberPath="NameA"
              ItemsSource= "{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=my:MarketsView},Path=DataContext.Markets}"
              SelectedValue="{Binding MarketCodeType.Market.MarketID, Mode=TwoWay}"
              SelectedValuePath="MarketID"/>
</DataTemplate>