我本以为这个问题已经得到了回答,但我没有找到它。我有一个简单的类:
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 开发人员在这么多年后不知道如何实现这样的字典。
您正在使用SelectedValuePath
和SelectedValue
错误。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] 删除。
我来说两句