0

我有一个组合框,需要根据另一个组合框的选择来填充。当我将模式设置为双向时,我得到一个异常,说明属性查询名称是只读的。我现在已经在这个问题上花了几个小时。我在做正确的事吗?有任何想法吗?

我的观点是这样的:

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

1 回答 1

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

        }
于 2013-06-14T16:05:25.403 回答