wpf DataGrid MVVM中的2个相关组合框

时间:2013-09-13 19:45:16

标签: c# wpf mvvm datagrid

有没有人有一个将2个相关ComboBox绑定到DataGrid的Selected Item的示例?我试过了,但我似乎无法让它发挥作用。我对MVVM很新,所以我做错了。

我有一个数据网格绑定到我的ViewModel中名为NewClient的对象中的集合 ClientMarketCodesClientMarketCodes有一个名为MarketCodeTypes的导航属性,它具有市场导航属性。所以我想在datagrid中显示代码MarketCodeTyeNameMarketName。 我有管理在DataGrid组合框中显示MarketsMarketCodeTypes,但我想在MarketsMaketCodeTypes之间建立关系,其中用户只能选择MarketCodeTypes相关那里Market。 我已将选定的项目添加到Markets Combobox,其中有关更改它的MarketCodeTypes集合的执行填充方法绑定到MarketCodeTypes Combobox,但由于过滤器适用于所有{{1}行,因此无法正常工作1}} Combobox列。 我在两个组合框中都发生了一些事情,例如在更改一行的MarketCodetypes时,组合框显示我选择另一个市场的所有其他市场并点击该行不受影响并返回其原始Market.i不知道为什么会这样。

继承我的ViewModel:

Markets

这是我的Xaml:

public class MarketsViewModel : ObservableObject, IMarketsViewModel, INavigationAware, IConfirmNavigationRequest, IRegionMemberLifetime
{
    #region MarketsViewModel

    public MarketsViewModel()
    {
        //NewClient.ClientMarketCodes[0].MarketCodeType.MarketID
        //MarketCodeTypes = new ObservableCollection<MarketCodeType>();
        this.GetMarkets();
        //this.GetMarketCodeTypes();
        this.PropertyChanged += MarketssViewModel_PropertyChanged;
    }

    private void MarketssViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        switch (e.PropertyName)
        {
            case "selectedMarket":
            {
                if (selectedMarket != null)
                {
                    GetMarketCodeTypes();
                }

                break;
            }
        }
    }

    #endregion

    #region --Members--

    private ObservableCollection<Market> m_Market;
    private ObservableCollection<MarketCodeType> m_MarketCodeType;
    private List<MarketCodeType> m_MarketCodeTypes;

    public List<MarketCodeType> MarketCodeTypesList
    {
        get
        {
            if (m_MarketCodeTypes == null)
            {
                m_MarketCodeTypes = new List<MarketCodeType>();
            }

            return m_MarketCodeTypes;
        }
        set
        { 
            m_MarketCodeTypes = value;

            RaisePropertyChanged("MarketCodeTypesList");
            RaisePropertyChanged("MarketCodeTypes");
        }
    }

    public ObservableCollection<MarketCodeType> MarketCodeTypes
    {
        get
        {
            if (MarketCodeTypesList != null)
                return new ObservableCollection<MarketCodeType>(MarketCodeTypesList);
            else
                return new ObservableCollection<MarketCodeType>();
        }
    }

    #endregion

    #region --Properties--

    public ObservableCollection<Market> Markets
    {
        get { return m_Market; }
        set
        {
            m_Market = value;
            RaisePropertyChanged("Markets");
        }
    }

    private Market _selectedMarket;
    public Market selectedMarket
    {
        get { return _selectedMarket; }
        set
        {
            if (_selectedMarket != value)
            {
                _selectedMarket = value;
                RaisePropertyChanged("selectedMarket");
                RaisePropertyChanged("MarketCodeTypes");
            }
        }
    }

    #endregion

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

    private void GetMarketCodeTypes()
    {
        try
        {
            MarketCodeType[] MarketCodeTypeArr;
            using (var client = new ClientServiceProxy())
            {
                MarketCodeTypeArr = client.GetAllMarketCodeTypes().Where(c=>c.MarketID==selectedMarket.marketID).ToArray();
            }
            if (MarketCodeTypeArr != null)
            {
                MarketCodeTypesList = MarketCodeTypeArr.ToList();
            }
            else
            { MarketCodeTypesList = new List<MarketCodeType>(); }

        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }
}`

0 个答案:

没有答案