在我的视图模型中ItemSsource更改后刷新Combobox

时间:2013-06-14 15:48:19

标签: c# wpf data-binding combobox

我有一个组合框,需要根据另一个组合框的选择进行填充。 当我将Mode设置为TwoWay时,我得到一个例外,说明Property QueryNames是ReadOnly。我现在在这个问题上花了好几个小时。我做对了吗?任何想法?

我的观点如下:

<StackPanel Orientation="Horizontal" Height="Auto" Name="stackPanel1" Width="Auto" Grid.Row="1">   
    <Label Content="Select Module:" Height="30" Name="labelModule" Width="Auto"></Label>
    <ComboBox Height="24" Name="comboBoxModule" Width="150" ItemsSource="{Binding QueryModules}" SelectedItem="{Binding SelectedQueryModule}" SelectionChanged="comboBoxModule_SelectionChanged" />
    <Label Content="Select the query you wish to run:" Height="30" Name="labelQuery" Width="Auto" Visibility="Collapsed" />
    <ComboBox Height="Auto" Name="comboBoxQuery" Width="300" IsEditable="True" ItemsSource="{Binding QueryNames, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" SelectedItem="{Binding SelectedQueryNames, Mode=TwoWay}" SelectedValuePath="Key" DisplayMemberPath="Value" Visibility="Collapsed" />
    <Button Content="Run Query" Height="23" Name="ButtonQuery" Width="75" Visibility="Collapsed"/>
</StackPanel>

我的观点模型在这里:

    public QueriesViewModel()
    {
        _service = new QueriesService();

        var queryModules = _service.GetQueryGroups();
        m_queryModules = new ObservableCollection<string>(queryModules);
        m_ReadOnlyQueryModules = new ReadOnlyObservableCollection<string>(m_queryModules); 
    }

    private readonly IQueriesService _service;

    #region QueryModules
    private readonly ObservableCollection<string> m_queryModules;
    private readonly ReadOnlyObservableCollection<string> m_ReadOnlyQueryModules;
    private string m_SelectedQueryModule;        

    public string SelectedQueryModule
    {
        get 
        {
            return m_SelectedQueryModule;
        }            
        set
        {
            if (m_SelectedQueryModule != value)
            {
                m_SelectedQueryModule = value;
                OnPropertyChanged("SelectedQueryModule");

                var queryNames = _service.GetQueryNames(m_SelectedQueryModule);
                m_queryNames = new Dictionary<int, string>(queryNames);
                m_ReadOnlyQueryNames = new Dictionary<int, string>(m_queryNames);
                OnPropertyChanged("SelectedQueryNames");
                //this.QueryNames = m_ReadOnlyQueryNames; //NOTE: Unable to do this, throws an exception stating the QueryNames property is read only.
            }
        }
    }

    public ReadOnlyObservableCollection<string> QueryModules { get { return m_ReadOnlyQueryModules; } }
    #endregion 

    #region QueryNames
    private Dictionary<int, string> m_queryNames;
    private Dictionary<int, string> m_ReadOnlyQueryNames;
    private string m_SelectedQueryNames;

    public string SelectedQueryNames
    {
        get
        {
            return m_SelectedQueryNames;
        }
        set
        {
            if (m_SelectedQueryNames != value)
            {
                m_SelectedQueryNames = value;
                OnPropertyChanged("SelectedQueryNames");
            }
        }
    }

    public Dictionary<int, string> QueryNames { get { return m_ReadOnlyQueryNames; } }
    #endregion

1 个答案:

答案 0 :(得分:0)

我认为您只需要为PropertyChanged属性

举起QueryNames个事件
 if (m_SelectedQueryModule != value)
        {
            m_SelectedQueryModule = value;
            OnPropertyChanged("SelectedQueryModule");

            var queryNames = _service.GetQueryNames(m_SelectedQueryModule);

            m_queryNames = new Dictionary<int, string>(queryNames);

            //RAISE PROPERTY CHANGED AFTER YOU SET NEW VALUE FOR m_queryNames
            OnPropertyChanged("QueryNames");

            m_ReadOnlyQueryNames = new Dictionary<int, string>(m_queryNames);
            OnPropertyChanged("SelectedQueryNames");

        }