0

別のコンボボックスの選択に基づいて設定する必要があるコンボボックスがあります。モードを TwoWay に設定すると、プロパティ 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
4

1 に答える 1