Silverlight MVVM - 在Listbox上没有触发Twoway绑定

时间:2010-08-30 16:26:30

标签: silverlight binding mvvm-light

在Silverlight MVVMLight 4.0应用程序中,我有一个列表框,一个文本框和一个复选框。 列表框的ItemsSource绑定到viewmodel中的对象列表。 列表框的SelectedItem是双向绑定到viewmodel中的对象(SelectedActivity)。

文本框的Text和复选框的IsSelected属性都与viewmodel中的SelectedActivity对象(Name和Selected属性)双向绑定。 没有代码隐藏。

这样可以正常工作:更改文本框中的名称或选中/取消选中复选框,然后使用Tab键更改对象的基础属性。

但是当我更改名称(或选中状态)然后立即单击列表中的其他项目时,更改未注册。

有人有解决方法吗?

亲切的问候,

Karel

这是XAML:

<ListBox Height="251" HorizontalAlignment="Left" Margin="11,39,0,0" Name="activitiesListBox" ItemsSource="{Binding Activities.Items}" VerticalAlignment="Top" Width="139"
             SelectedItem="{Binding Activities.SelectedActivity, Mode=TwoWay}">

这是包含绑定到列表的项目的Activities类:

public class CLJActivitiesViewModel : ViewModelBase
{
    /// <summary>
    /// Initializes a new instance of the ActivitiesViewModel class.
    /// </summary>
    public CLJActivitiesViewModel()
    {
        ////if (IsInDesignMode)
        ////{
        ////    // Code runs in Blend --> create design time data.
        ////}
        ////else
        ////{
        ////    // Code runs "for real": Connect to service, etc...
        ////}
    }


    #region items
    /// <summary>
    /// The <see cref="Items" /> property's name.
    /// </summary>
    public const string ItemsPropertyName = "Items";

    private ObservableCollection<CLJActivityViewModel> m_Items = null;

    /// <summary>
    /// Gets the Items property.
    /// TODO Update documentation:
    /// Changes to that property's value raise the PropertyChanged event. 
    /// This property's value is broadcasted by the Messenger's default instance when it changes.
    /// </summary>
    public ObservableCollection<CLJActivityViewModel> Items
    {
        get
        {
            return m_Items;
        }

        set
        {
            if (m_Items == value)
            {
                return;
            }

            var oldValue = m_Items;
            m_Items = value;

            RaisePropertyChanged(ItemsPropertyName, oldValue, value, true);
        }
    }
    #endregion

    #region SelectedActivity
    /// <summary>
    /// The <see cref="SelectedActivity" /> property's name.
    /// </summary>
    public const string SelectedActivityPropertyName = "SelectedActivity";

    private CLJActivityViewModel m_SelectedActivity = null;

    /// <summary>
    /// Gets the SelectedActivity property.
    /// TODO Update documentation:
    /// Changes to that property's value raise the PropertyChanged event. 
    /// This property's value is broadcasted by the Messenger's default instance when it changes.
    /// </summary>
    public CLJActivityViewModel SelectedActivity
    {
        get
        {
            return m_SelectedActivity;
        }

        set
        {
            if (m_SelectedActivity == value)
            {
                return;
            }

            var oldValue = m_SelectedActivity;
            m_SelectedActivity = value;

            RaisePropertyChanged(SelectedActivityPropertyName, oldValue, value, true);
        }
    }
    #endregion



    public override void Cleanup()
    {
        // Clean own resources if needed

        base.Cleanup();
    }
}        

2 个答案:

答案 0 :(得分:1)

我遇到了像TextBox这样的问题,但没有看到它影响复选框。 TextBox问题正在发生,因为绑定文本会更新然后焦点丢失。这就是为什么如果你先选择标签然后更改你的选择它会按预期工作。如果您更改选择,则直接绑定文本不会更新,因为焦点丢失消息来得太晚。

处理此问题的一种方法是每次用户在文本框中键入文本时强制更新绑定。您可以使自定义行为保持mvvm。

答案 1 :(得分:1)

我遇到了同样的问题。我不得不在用户输入文本时触发更新,以便我可以进行一些验证。

实现这一目标的一种简单方法是创建一个自定义行为,然后您可以将其添加到任何TextBox

我的如下:

public static class TextChangedBindingBehavior
{
    public static readonly DependencyProperty InstanceProperty =
        DependencyProperty.RegisterAttached("Instance", typeof(object), typeof(TextChangedBindingBehavior), new PropertyMetadata(OnSetInstanceCallback));


    public static object GetInstance(DependencyObject obj)
    {
        return (object)obj.GetValue(InstanceProperty);
    }

    public static void SetInstance(DependencyObject obj, object value)
    {
        obj.SetValue(InstanceProperty, value);
    }

    private static void OnSetInstanceCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textBox = d as TextBox;
        if (textBox != null)
        {
            textBox.TextChanged -= OnTextChanged;
            textBox.TextChanged += OnTextChanged;
        }
    }

    private static void OnTextChanged(object sender, TextChangedEventArgs e)
    {
        var textBox = (TextBox)sender;

        if(!DesignerProperties.GetIsInDesignMode(textBox))
        {
            textBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
        }
    }
}

并将其设置为TextBoxBehaviors是我将该类放在上面的命名空间):

 <TextBox Behaviors:TextChangedBindingBehavior.Instance="" Text="{Binding Name, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" />