简体   繁体   中英

changing selected value of Combobox in DataGrid wpf

Dears, i have a combobox column in a datagrid binded to a observable collection Markets in my ViewModel,while the DataGrid is binded to a Collection ClientMarketCode which is a collection in NewClient object. ClientMarketCode has this properties Code,MarketCodeTypeID,MarketID. so i used a combobox binded to Markets in case user wanted to edit the Market it shows all Markets and user select the market he wants.i have a problem in selecting any other market as the value of market in the selected row didnt change after selecting new market from combobox.

eg:

lets say that the first row has KSA market then i changed it from the combobox to USA after clicking on USA it returns baco to KSA i dont know y.

Here's my view model:

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

Heres the 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>

To summarize my comment, your binding should either be:

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

or (not so certain about that one):

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM