将 ObservableDictionary 绑定到 ComboBox (WPF)

泰勒摩尔

我本以为这个问题已经得到了回答,但我没有找到它。我有一个简单的类:

public class BinanceEndpoint : ObservableObject
    {
        private string baseAddress;
        private string socketBaseAddress;

        public string BaseAddress
        {
            get { return baseAddress; }
            set { baseAddress = value; RaisePropertyChangedEvent(nameof(BaseAddress)); }
        }

        public string SocketBaseAddress
        {
            get { return socketBaseAddress; }
            set { socketBaseAddress = value; RaisePropertyChangedEvent(nameof(SocketBaseAddress)); }
        }
    }

然后我用来自该类的对象填充 ObservableDictionary:

    private MainViewModel()
    {
 private BinanceEndpoint apiEndPoint;
        private ObservableDictionary<string, BinanceEndpoint> endPoints = new ObservableDictionary<string, BinanceEndpoint>();
        
        endPoints.Add("Binance.com", new BinanceEndpoint() { BaseAddress = "https://api.binance.com", SocketBaseAddress = "wss://stream.binance.com:9443", });
                    endPoints.Add("Binance.us", new BinanceEndpoint() { BaseAddress = 
    
        "https://api.binance.us", SocketBaseAddress = "wss://stream.binance.us:9443", });
                        endPoints.Add("Testnet", new BinanceEndpoint() { BaseAddress = "https://testnet.binance.vision", SocketBaseAddress = "wss://testnet.binance.vision", });
        }
        [JsonIgnore]
                public ObservableDictionary<string,BinanceEndpoint> EndPoints
                {
                    get { return endPoints; }
                    set { endPoints = value; RaisePropertyChangedEvent(nameof(EndPoints)); }
                }

public BinanceEndpoint APIEndPoint
        {
            get { return apiEndPoint; }
            set { apiEndPoint = value; RaisePropertyChangedEvent(nameof(APIEndPoint)); }
        }
    }

然后我尝试使用 ObservableDictionary 填充 ComboBox。

<ComboBox Grid.Column="1" ItemsSource="{Binding EndPoints}" DisplayMemberPath="Key" SelectedValuePath="Value" SelectedValue="{Binding Path=APIEndPoint, Mode=TwoWay}"/>

我遇到的问题是 SelectedValue 在加载时不会更新 ComboBox 的值。我究竟做错了什么?

仿生代码

您应该首先避免绑定到 a Dictionary自 2002 年发布以来,.NET 库中没有 ObservableDictionary 是有原因的。我想我们同意这不是因为 MS 开发人员在这么多年后不知道如何实现这样的字典。

您正在使用SelectedValuePathSelectedValue错误。SelectedValue返回SelectedValuePath指向的属性的值SelectedValuePath在 上提供属性路径SelectedItem
当您主动设置 时SelectedValue,控件将尝试查找并选择由 指定的项的属性SelectedValuePath与 的值匹配的项SelectedValue

SelectedItem返回所选项目(实例)时,然后SelectedValue返回SelectedItem使用SelectedValuePath.

例如:我们绑定到一个Dictionary<string, BinanceEndpoint>. 为了显示Key您的 ,Dictionary我们指定了DisplayMemberPath(作为 a 的替代DataTemplate)。
SelectedItem将举行KeyValuePair<string, BinanceEndpoint>
ComboBox.SelectedValue返回所选项目的SocketBaseAddress值,我们必须将 设置SelectedValuePath"Value.SocketBaseAddress"

<ComboBox x:Name="ComboBox" 
          ItemsSource="{Binding EndPoints}" 
          DisplayMemberPath="Key" 
          SelectedValuePath="Value.SocketBaseAddress" />

<!-- Display the selected item's SocketBaseAddress value -->
<TextBlock text="{Binding ElementName=ComboBox, Path=SelectedValue}" />

如果您希望SelectedValue返回BinanceEndpoint实例,则设置SelectedValuePath"Value"

<ComboBox x:Name="ComboBox" 
          ItemsSource="{Binding EndPoints}" 
          DisplayMemberPath="Key" 
          SelectedValuePath="Value" />

<!-- Display the selected item's 'SocketBaseAddress' value -->
<TextBlock text="{Binding ElementName=ComboBox, Path=SelectedValue.SocketBaseAddress}" />

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章