【问题标题】:WPF databinding and refreshing its UIWPF 数据绑定和刷新其 UI
【发布时间】:2020-07-20 04:55:31
【问题描述】:

我之前发布过同样的问题,但不清楚(并且包含太多自我引发的错误以试图修复代码)所以重新发布它并提供更多详细信息。

所以我有“MainUiWindow.xaml”文件,它使用这样的数据绑定:

            <ItemsControl x:Name="gridSettingsMonster" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding SettingsMonster}">
                <ItemsControl.ItemTemplate>                        
                    <DataTemplate DataType="{x:Type core:Setting}">
                        <Grid x:Name="gridMonster">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Label}" IsEnabled="{Binding Enabled}" ToolTip="{Binding Description}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10 5 10 5" FontWeight="{Binding Fontweight}" ></TextBlock>
                            <ts:ToggleSwitch x:Name="toggleSwitchMonsterAll" IsEnabled="{Binding Enabled}" Grid.Row="0" Grid.Column="1" Command ="{Binding TriggerAction}" IsChecked="{Binding Value}" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5 0 20 2" Foreground="White" UncheckedText="" CheckedText="" UncheckedBorderBrush="#FF333333" CheckedBorderBrush="#FF2D2D30"/>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

SettingsMonster 绑定:

        SettingsMonster.Add(new Setting(ConfigHelper.Main.Values.Overlay.MonsterWidget.IsVisible, true, "Monster_1", "Monster Widget", "Show/Hide Monsters Widget", new Command(_ =>
       {
           ConfigHelper.Main.Values.Overlay.MonsterWidget.IsVisible = !ConfigHelper.Main.Values.Overlay.MonsterWidget.IsVisible;
           ConfigHelper.Main.Save();
       })));


        SettingsMonster.Add(new Setting(ConfigHelper.Main.Values.Overlay.MonsterWidget.ShowUnchangedMonsters, ConfigHelper.Main.Values.Overlay.MonsterWidget.IsVisible,  "Monster_2", "    Show unchanged monsters", "Automatically hide monsters if they are not damaged", new Command(_ =>
       {
           ConfigHelper.Main.Values.Overlay.MonsterWidget.ShowUnchangedMonsters = !ConfigHelper.Main.Values.Overlay.MonsterWidget.ShowUnchangedMonsters;
           ConfigHelper.Main.Save();
       })));

最后是设置类:

 public class Setting 
    {
        public bool Value { get; set; }
        public bool Enabled { get; set; }
        public string Name { get; }
        public string Label { get; }
        public string Description { get; }
        public string Checkbox_visibility { get; }
        public string Fontweight { get; }
        public List<Setting>SubSettings { get; }
        public Command TriggerAction { get; }
        public Setting(bool value, bool enabled, string name, string label, string description, Command action = null)
        {
            Value = value;
            Enabled = enabled;
            Name = name;
            Label = label;
            Description = description;                       
            SubSettings = new List<Setting>(); 
            TriggerAction = action;            
        }
    }

问题: 当我运行构建并使用“ToggleSwitch”(它基本上是一个开源复选框)来更改“ConfigHelper.Main.Values.Overlay.MonsterWidget.IsVisible”的值时,它会正确取消选中 UI。 我希望此复选框也可以控制其他复选框(即“Monster_2”),以便在关闭主复选框时,将子复选框/文本块的 IsEnabled 值设置为 FALSE。 我到了这样一个阶段,如果我检查主复选框,重新启动构建,然后子复选框/文本块都设置为 IsEnabled=False。但是,我希望同样的事情实时发生(即刷新 UI 而无需重新启动)。

任何帮助将不胜感激。

编辑 1.

所以我尝试在我的 Settings 类中实现 INotifyPropertyChanged,如下所示:

public class Setting : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private bool _value;
        private bool _enabled;
        
        public bool Value
        {
            get
            {
                return _value;
            }
            set
            {
                if (_value == value)
                    return;
                _value = value;
                OnPropertyChanged(nameof(Enabled));

            }
        }
        public bool Enabled
        {
            get
            {
                return _enabled;
            }
            set
            {
                if (_enabled == value)
                    return;
                _enabled = value;
                OnPropertyChanged(nameof(Enabled));

            }
        }

        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }                
        }

        public string Name { get; }
        public string Label { get; }
        public string Description { get; }
        public string Checkbox_visibility { get; }
        public string Fontweight { get; }
        public List<Setting>SubSettings { get; }
        public Command TriggerAction { get; }
        public Setting(bool value, bool enabled, string name, string label, string description, Command action = null)
        {
            Value = value;
            Enabled = enabled;
            Name = name;
            Label = label;
            Description = description;                       
            SubSettings = new List<Setting>(); 
            TriggerAction = action;            
        }

但是我的 UI 仍然没有刷新...有什么帮助吗?

【问题讨论】:

  • 尝试在设置类型中实现 INotifyPropertychanged。
  • 尝试在设置类型中实现 INotifyPropertychanged...但仍然不刷新。我的更改已添加到 EDIT1。
  • INotifyPropertyChangedValue 属性的实现存在错误。当它看起来应该是 OnPropertyChanged(nameof(Value)) 时,您调用 OnPropertyChanged(nameof(Enabled))

标签: c# wpf data-binding


【解决方案1】:

我的视图模型首先引用了不正确的变量。 我在我的设置对象中实现了 INotifyPropertyChange,如下所示,还添加了一个在触发复选框时运行的命令。

public class Setting : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

    private bool _value;
    private bool _enabled;
    
    public bool Value
    {
        get
        {
            return _value;
        }
        set
        {
            if (_value == value)
                return;
            _value = value;
            OnPropertyChanged(nameof(Value));
        }
    }
    public bool Enabled
    {
        get
        {
            return _enabled;
        }
        set
        {
            if (_enabled == value)
                return;
            _enabled = value;
            OnPropertyChanged(nameof(Enabled));
        }
    }

   
    public string Name { get; }
    public string Label { get; }
    public string Description { get; }        
    public string Fontweight { get; }
    public List<Setting>SubSettings { get; }
    public Command TriggerAction { get; }
    public Setting(bool _value, bool _enabled, string name, string label, string description, Command action = null)
    {
        Value = _value;
        Enabled = _enabled;
        Name = name;
        Label = label;
        Description = description;                       
        SubSettings = new List<Setting>(); 
        TriggerAction = action;            
    }                
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2021-10-20
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多