窗口打开时 WPF 绑定(bind)不正确

标签 wpf binding postsharp

我正在编写一个带有单个窗口的小型 WPF 实用程序,到目前为止,其代码如下。我使用 PostSharp 自动处理属性更改通知,并且当我在组合框中选择不同的组织时,绑定(bind)会更新。但是,当窗口第一次打开时,该行

organisationComboBox.SelectedIndex = 0;

执行,绑定(bind)不会更新。在organizationComboBox_SelectionChanged()方法中设置的断点显示,所选项目的_lightningLocationsEnabled变量被设置为true,并且依赖于它的属性设置正确,但属性绑定(bind)的控件最初显示的值是如果变量为假。

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);
        }
    }
}

这是绑定(bind)控件的 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"/>

最佳答案

将 BindOrganizations 放入窗口的 Loaded 事件中。

不要忘记 Loaded 事件可以被多次调用,您可以使用 bool 标志。

关于窗口打开时 WPF 绑定(bind)不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33650612/

相关文章:

c# - 库还是自己的框架?

c - 从 c 运行整个 lua 脚本

WPF 基于属性绑定(bind) CombobBoxItem

c# - 根据现有属性更改代码?

.net - 更改分层 TreeView 中的突出显示颜色

wpf - 如何使用固定宽度的列修复gridview中重叠的垂直滚动条

c# - 重复控制 n 次并绑定(bind)每个实例

python - 元 python : Adding Methods to a Class

c# - PostSharp - il 编织 - 想法

c# - msbuild 使用了错误的程序集名称