【问题标题】:WPF Combobox initial dictionary binding value not showingWPF Combobox初始字典绑定值未显示
【发布时间】:2013-05-31 05:09:06
【问题描述】:

我有一个 wpf 组合框绑定到类 SInstance 的属性 LogicalP。组合框的 ItemSource 是一个字典,其中包含 LogicalP 类型的项。

如果我将 SInstance 中的 LogicalP 设置为初始状态,则组合框文本字段显示为空。如果我选择下拉菜单,我所有的字典值都在那里。当我更改 SInstance 中的选择时,LogicalP 会正确更新。如果我在 C# 中更改 Sinstance,则相应的组合框值不会反映下拉菜单中更新的 LogicalP。

我已将绑定模式设置为双向,但没有运气。有什么想法吗?

我的 Xaml:

<UserControl.Resources>
<ObjectDataProvider x:Key="PList"
                    ObjectType="{x:Type src:MainWindow}"
                    MethodName="GetLogPList"/>
</UserControl.Resources>

<DataTemplate DataType="{x:Type src:SInstance}">
<Grid>
    <ComboBox ItemsSource="{Binding Source={StaticResource PList}}"
              DisplayMemberPath ="Value.Name" 
              SelectedValuePath="Value"
            SelectedValue="{Binding Path=LogicalP,Mode=TwoWay}">
    </ComboBox>
</Grid>
</DataTemplate>

我的 C#:

public Dictionary<string, LogicalPType> LogPList { get; private set; }
public Dictionary<string, LogicalPType> GetLogPList()
{
    return LogPList;
}

public class LogicalPType
{
    public string Name { get; set; }
    public string C { get; set; }
    public string M { get; set; }
}                  

public class SInstance : INotifyPropertyChanged
{
    private LogicalPType _LogicalP;

    public string Name { get; set; }
    public LogicalPType LogicalP
    {
        get { return _LogicalP; }
        set
        {
            if (_LogicalP != value)
            {
                _LogicalP = value;
                NotifyPropertyChanged("LogicalP");
            }
        }
    }

    #region INotifyPropertyChanged Members
    #endregion
}    

【问题讨论】:

    标签: wpf xaml binding dictionary combobox


    【解决方案1】:

    他们查看的来源不同。
    您需要让 SInstance 同时提供 LogPList 和 LogicalP。

    _LogicalP 未连接到 LogPList

    如果你想让不同的对象比较相等,那么你需要重写 Equals。

    【讨论】:

    • 我不确定您是否理解这个问题。当我更改组合框上的选择时,底层绑定对象(SInstance)得到更新,它保持空白。但是,当我更改 SInstance 中的属性时,组合框不会更新以反映所选项目。
    • 是的,我明白。展示如何在 C# 中为 LogicalP 赋值。
    • 抱歉,我没有显示在主表单加载时加载 LogPList 字典的代码。我确实找到了问题的解决方案,它允许绑定双向进行。我会将其修改为我的原始帖子。
    • 我将 GetLogPList 方法作为属性移到了类 SInstance 中。这允许绑定是双向的。我不太清楚为什么。
    • 如果这回答了问题,然后用检查表示。
    【解决方案2】:

    这是我的工作解决方案。通过将字典检索 GetLogPList 移动到与提供数据的类相同的类(如 Blam 建议的那样),我能够使绑定以两种方式工作。我将绑定更改为列表而不是字典以简化组合框

    这是更新后的 Xaml,显示了新的 ItemsSource 绑定和 SelectedValuePath 的删除:

    <DataTemplate DataType="{x:Type src:SInstance}">
        <Grid>
            <ComboBox ItemsSource="{Binding GetLogPList}"
                      DisplayMemberPath ="Name" 
                    SelectedValue="{Binding Path=LogicalP,Mode=TwoWay}">
            </ComboBox>
        </Grid>
    </DataTemplate>
    

    然后我将字典 LogPList 更改为静态,以便类 SInstance 可以访问它:

        public static Dictionary<string, LogicalPType> LogPList { get; private set; }
    

    最后,我将 GetLogPList 作为属性移到了类 SInstance 中。再次注意它返回一个列表而不是字典,以使 Xaml 更简单:

    public class SInstance : INotifyPropertyChanged
    {
        public List<LogicalPType> GetLogPList
        {
            get { return MainWindow.LogPList.Values.ToList(); }
            set { }
        }   
    
        private LogicalPType _LogicalP;
    
        public string Name { get; set; }
        public LogicalPType LogicalP
        {
            get { return _LogicalP; }
            set
            {
                if (_LogicalP != value)
                {
                    _LogicalP = value;
                    NotifyPropertyChanged("LogicalP");
                }
            }
        }
    
        #region INotifyPropertyChanged Members
        #endregion
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 2020-01-24
      • 2014-08-27
      • 2023-03-02
      • 2015-05-06
      相关资源
      最近更新 更多