gpt4 book ai didi

WPF MVVM Light - SelectedItem 的绑定(bind)没有改变

转载 作者:行者123 更新时间:2023-12-03 10:24:21 24 4
gpt4 key购买 nike

这个领域有很多帖子,但没有一个对我有帮助......这是场景:我有两个“季节”下拉菜单来模拟一个范围。如果您在开始范围内选择一个季节,viewmodele 会自动将绑定(bind)到结束范围的属性设置为同一季节(因此它默认为一年而不是范围。这就是 XAML 的样子(删除了很​​多格式化属性以提高可读性):

<ComboBox ItemsSource="{Binding AvailableSeasons, Mode=OneWay}" 
SelectedItem="{Binding SelectedBeginRangeSeason, Mode=TwoWay}"
ItemTemplate="{DynamicResource SeasonItemShortFormat}" />
<ComboBox ItemsSource="{Binding AvailableSeasons, Mode=OneWay}"
SelectedItem="{Binding SelectedEndRangeSeason, Mode=TwoWay}"
ItemTemplate="{DynamicResource SeasonItemShortFormat}" />

View 模型中的属性如下所示:
private Season _selectedBeginRangeSeason;
private const string SelectedBeginRangeSeasonPropertyName = "SelectedBeginRangeSeason";
public Season SelectedBeginRangeSeason {
get { return _selectedBeginRangeSeason; }
set {
if (_selectedBeginRangeSeason != value) {
var oldValue = _selectedBeginRangeSeason;
_selectedBeginRangeSeason = value;

RaisePropertyChanged<Season>(SelectedBeginRangeSeasonPropertyName, oldValue, value, true);
}
}
}

private Season _selectedEndRangeSeason;
private const string SelectedEndRangeSeasonPropertyName = "SelectedEndRangeSeason";
public Season SelectedEndRangeSeason {
get { return _selectedEndRangeSeason; }
set {
if (_selectedEndRangeSeason != value) {
Debug.WriteLine("Updating property SelectedEndRangeSeason...");

var oldValue = _selectedEndRangeSeason;
_selectedEndRangeSeason = value;

Debug.WriteLine("Broadcasting PropertyChanged event for property SelectedEndRangeSeason...");
RaisePropertyChanged<Season>(SelectedEndRangeSeasonPropertyName, oldValue, value, true);
}
}
}

private void UpdateSelectedSeasonSelectors() {
// if the end range isn't selected...
if (_selectedEndRangeSeason == null) {
// automatically select the begin for the end range
SelectedEndRangeSeason = _selectedBeginRangeSeason;
}
}

我已经通过调试语句和单元测试验证了 end 属性正在更改,但是当我选择它时 UI 并没有改变......无法弄清楚发生了什么并查看了这么多不同方法...

最佳答案

你收到了SelectedSeason来自 AvailableSeasons收藏?如果没有,您是否实现了一些特别的措施来比较季节?

例如,假设您有

<ComboBox ItemsSource="{Binding AvailableSeasons}" 
SelectedItem="{Binding SelectedSeason}" />

如果 SelectedSeason = new Season(); SelectedItem 绑定(bind)不起作用,因为 new Season();AvailableSeasons 中不存在.

您需要设置 SelectedSeason = AvailableSeasons[x]为 SelectedItem 工作,因为这使两个项目完全相同。或者您可以实现一些自定义方法来比较两个季节,看看它们是否相同。通常我只是覆盖 ToString()被比较的类的方法。

关于WPF MVVM Light - SelectedItem 的绑定(bind)没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6705866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com