【问题标题】:Combobox binded to ViewModel property does not updates when the property changes绑定到 ViewModel 属性的组合框在属性更改时不会更新
【发布时间】:2011-09-19 09:13:00
【问题描述】:

我有一个 ComboBox,其中填充了通过 ItemSource 枚举的可用项,并绑定到 ViewModel 属性 WorkingMode:

<ComboBox ItemsSource="{Binding Source={StaticResource WorkingModeEnum}}" SelectedItem="{Binding Path=WorkingMode, Mode=TwoWay" />

如果我手动更改 ComboBox 的选定项,ViewModel 属性会按预期更改其值,但如果我更改 ViewModel 的属性值,ComboBox 不会更改它向用户显示的内容。

ViewModel 实现 INotifyPropertyChanged 并在值更改时引发事件。更重要的是,通过以下方式激活 WPF 日志记录:

diag:PresentationTraceSources.TraceLevel=High

当 ViewModel 属性将其值更改为 All(枚举值之一)时,我得到以下信息:

System.Windows.Data Warning: 86 : BindingExpression (hash=50934842): Update - got raw value 'All'
System.Windows.Data Warning: 89 : BindingExpression (hash=50934842): Update - implicit converter produced 'All'
System.Windows.Data Warning: 90 : BindingExpression (hash=50934842): Update - using final value 'All'

所以看起来 ComboBox 收到了新值,但它没有任何变化(它仍然显示最后一个值,而不是 All)。

任何帮助都会很棒!谢谢。

我的代码:

App.xaml:

 <Application x:Class="TestCombo.App"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 Startup="OnStartup">
 <Application.Resources>

 </Application.Resources>
 </Application>

View.xaml:

<Window x:Class="TestCombo.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:TestCombo="clr-namespace:TestCombo"
Title="View" Height="300" Width="300">

<Window.Resources>
    <ObjectDataProvider x:Key="TestEnum" MethodName="GetNames" ObjectType="{x:Type System:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="TestCombo:TestEnum"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

<Grid>
    <StackPanel>
        <ComboBox ItemsSource="{Binding Source={StaticResource TestEnum}}" SelectedItem="{Binding Path=Test, Mode=TwoWay, PresentationTraceSources.TraceLevel=High}"/>
        <Button Height="23" Name="button1" VerticalAlignment="Bottom" Click="button1_Click">Button</Button>
        <TextBlock Grid.Row="2" Text="{Binding Test, Mode=OneWay}"/>
    </StackPanel>
</Grid>

View.xaml.cs:

using System.Windows;

namespace TestCombo
{
public partial class View : Window
{
    public View()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        ((ViewModel) this.DataContext).Test = TestEnum.Three;
    }
}
}

ViewModel.cs:

using System.ComponentModel;

namespace TestCombo
{
public class ViewModel : INotifyPropertyChanged
{
    public Model Model { get; private set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public ViewModel()
    {
        this.Model = new Model();
    }

    public TestEnum Test
    {
        get
        {
            return this.Model.Test;
        }
        set
        {
            if (this.Model.Test != value)
            {
                this.Model.Test = value;
                this.OnPropertyChanged("Test");
            }
        }

    }
    protected virtual void OnPropertyChanged(string name)
    {
        var handler = this.PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}
}

模型.cs:

namespace TestCombo
{
public class Model
{
    public TestEnum Test { get; set; }
}

public enum TestEnum
{
    Zero = 0,
    One = 1,
    Two = 2,
    Three = 3,
    Four = 4
}
}

【问题讨论】:

    标签: wpf data-binding combobox binding


    【解决方案1】:

    请发布您的 VM 代码。我创建了一个测试项目,一切正常。

    示例代码:

    <UserControl x:Class="WpfStackoverflow.ComboboxEnumSelectedValue"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:System="clr-namespace:System;assembly=mscorlib" 
             xmlns:local="clr-namespace:WpfStackoverflow" mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <ObjectDataProvider x:Key="WorkingModeEnum" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:WorkingMode"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </UserControl.Resources>
    <StackPanel>
        <Button Content="Test Set All" Click="Button_Click" />
        <ComboBox ItemsSource="{Binding Source={StaticResource WorkingModeEnum}}" SelectedItem="{Binding Path=SelectedWorkingMode, Mode=TwoWay}"/>
        <TextBlock Text="{Binding SelectedWorkingMode, Mode=OneWay}"/>
    </StackPanel>
    

    .cs

    public partial class ComboboxEnumSelectedValue : UserControl
    {
        private WorkingVM vm = new WorkingVM();
        public ComboboxEnumSelectedValue()
        {
            InitializeComponent();
            this.DataContext = vm;
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            vm.SetAllModeForTesting();
        }
    
    
    }
    

    枚举:

    public enum WorkingMode
    {
        Mode1,
        Mode2,
        Mode3,
        All
    }
    

    虚拟机:

    public class WorkingVM : INotifyPropertyChanged
    {
        private WorkingMode _selectedmode;
    
        public void SetAllModeForTesting()
        {
            this.SelectedWorkingMode = WorkingMode.All;
        }
    
        public WorkingMode SelectedWorkingMode
        {
            get { return _selectedmode; }
            set { _selectedmode = value;
            this.OnPropertyChanged("SelectedWorkingMode");
            }
        }
    
        #region Implementation of INotifyPropertyChanged
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void OnPropertyChanged(string propertyname)
        {
            var handler = PropertyChanged;
    
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyname));
            }
        }
    
        #endregion
    }
    

    【讨论】:

    • 您的样本有效,我的无效。我正在比较它们,我看不出有什么区别。现在正在努力。
    • 我明白了,我使用的是 MethodName="GetNames" 而不是 MethodName="GetValues"。不知道有什么不同的想法。
    【解决方案2】:

    你需要设置UpdateSourceTrigger=PropertyChanged

    SelectedItem="{Binding WorkingMode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    

    希望对你有帮助

    这会给你一个详细的解释 UpdateSourceTrigger – DataBinding

    【讨论】:

    • 我试过了,但没有帮助,还是谢谢。在 ComboBox 中,默认值为 PropertyChanged(在 TextBox 中为 LostFocus)。
    猜你喜欢
    • 2014-04-16
    • 1970-01-01
    • 2013-11-02
    • 2019-03-24
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    相关资源
    最近更新 更多