【发布时间】:2015-11-11 12:31:51
【问题描述】:
我正在编写一个带有单个窗口的小型 WPF 实用程序,到目前为止,它的代码如下。我正在使用 PostSharp 自动处理属性更改通知,并且当我在组合框中选择不同的组织时,绑定正在更新。但是,当窗口第一次打开和行时
organisationComboBox.SelectedIndex = 0;
执行,绑定不更新。在organizationComboBox_SelectionChanged() 方法中设置的断点表明,对于所选项目,_lightningLocationsEnabled 变量设置为 true,并且依赖于它的属性设置正确,但绑定属性的控件最初显示的值如果变量为假。
PostSharp 文档建议不需要手动强制执行属性更改通知,因此我认为我做错了什么。谁能帮我找出问题所在?
这里是代码
using DynaMiX.Common.Groups.Constants;
using DynaMiX.Data.Operations;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using PostSharp.Patterns.Model;
namespace LightningMapLocationToolGUI
{
[NotifyPropertyChanged]
public partial class MainWindow : Window
{
private IOrganisationRepository _organisationRepository = new OrganisationRepository();
private bool _lightningLocationsEnabled;
public MainWindow()
{
InitializeComponent();
BindOrganisations();
}
public string StatusText => $"Lightning Map Locations feature is { (_lightningLocationsEnabled ? "ENABLED" : "DISABLED") }";
public Brush StatusColorBrush => new SolidColorBrush(_lightningLocationsEnabled ? Colors.Green : Colors.Red);
public string EnableButtonText => _lightningLocationsEnabled ? "Disable" : "Enable";
private void BindOrganisations()
{
var orgList = _organisationRepository
.GetOrganisationSummaries()
.Where(o => o.DatabaseState == DatabaseState.Active)
.OrderBy(o => o.Name)
.ToList();
organisationComboBox.ItemsSource = orgList;
organisationComboBox.DisplayMemberPath = "Name";
organisationComboBox.SelectedValuePath = "OrganisationId";
organisationComboBox.SelectedIndex = 0;
}
private void organisationComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
_lightningLocationsEnabled = _organisationRepository.LightningLocationsEnabledFor((long)organisationComboBox.SelectedValue);
}
}
}
这里是绑定控件的 XAML
<Label x:Name="statusLabel" Content="{Binding StatusText}" TextBlock.Foreground="{Binding StatusColorBrush}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="25,71,0,0"/>
<Button x:Name="enableButton" Content="{Binding EnableButtonText}" Visibility="{Binding ControlVisibility}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="309,74,0,0"/>
【问题讨论】:
-
尝试将BindOrganisations放在Window的Loaded事件中。
-
谢谢,确实解决了问题。也许您想将其添加为答案。
-
不客气,已添加答案。谢谢,再见