将自定义控件中的DependencyProperty绑定到ViewModel属性

时间:2016-11-18 08:40:51

标签: c# uwp mvvmcross uwp-xaml

我在我的跨平台原生Xamarin应用中使用MVVMCross。我似乎有一个问题,我的自定义控件中的布尔属性绑定到我的ViewModel中的布尔属性。例如:

我的自定义控件BarCodeTextBox.cs:

public sealed class BarCodeTextBox : TextBox
{

    public BarCodeTextBox()
    {
        this.DefaultStyleKey = typeof(BarCodeTextBox);
    }

    public bool IsListeningForCodes
    {
        get { return (bool)GetValue(IsListeningForCodesProperty); }
        set {
            SetValue(IsListeningForCodesProperty, value);
            if (value)
            {
                IsReadOnly = true;
                PrefixElement.Visibility = Visibility.Collapsed;
                Window.Current.CoreWindow.CharacterReceived += CoreWindow_CharacterReceived;
            }
            else
            {
                IsReadOnly = false;
                Focus(FocusState.Keyboard);
                PrefixElement.Visibility = Visibility.Visible;
                Window.Current.CoreWindow.CharacterReceived -= CoreWindow_CharacterReceived;
            }

            Text = string.Empty;
        }
    }


    // Using a DependencyProperty as the backing store for IsListening.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsListeningForCodesProperty =
        DependencyProperty.Register("IsListeningForCodes", typeof(bool), typeof(BarCodeTextBox), new PropertyMetadata(false));

BoxCloseViewModel.cs页面的viewmodel:

public class BoxCloseViewModel : ViewModelBase
{        
    private bool m_isManualBoxCodeInput;
    public bool IsManualBoxCodeInput
    {
        get { return m_isManualBoxCodeInput; }
        set { SetProperty(ref m_isManualBoxCodeInput, value); }
    }
}

BoxCloseView.xaml中的绑定:

<Controls:BarCodeTextBox x:Name="BarCodeInput" Text="{Binding BoxCode, Mode=TwoWay}" IsListeningForCodes="{Binding IsManualBoxCodeInput, Mode=OneWay, Converter={StaticResource BoolToInverseBool}}"/>

当我在ViewModel中更改IsManualBoxCodeInput的值时,控件中没有任何反应(IsListeningForCodes不会更改)。我检查过的事情:

  • 转换器工作正常(并且移除它不能解决问题)。事实上,当ViewModel属性发生变化时,我会调用转换器(我可以调试它)。
  • 当我在页面后面的代码中更改IsListeningForCodes的值时,它可以正常工作(控件显示更改)。
  • 当我使用字符串属性做同样的事情时,一切都很完美。
  • 输出日志中没有绑定错误。
  • 使用IsManualBoxCodeInput属性正确触发PropertyChangedEvent。
  • 我意识到同样的事情发生在另一个带有布尔属性的控件上,在迁移到MVVMCross后不再有用。

1 个答案:

答案 0 :(得分:0)

绑定不会调用DependencyObject的setter属性。如果您希望在绑定更改时执行某些代码,则需要将其添加为PropertyMetadata的回调。

public bool IsListeningForCodes
{
    get { return (bool)GetValue(IsListeningForCodesProperty); }
    set { SetValue(IsListeningForCodesProperty, value); }
}

public static readonly DependencyProperty IsListeningForCodesProperty =
    DependencyProperty.Register("IsListeningForCodes", typeof(bool), typeof(BarCodeTextBox), 
    new PropertyMetadata(false, OnIsListeningForCodesChanged));

static void OnIsListeningForCodesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var instance = (BarCodeTextBox)d;
    instance.OnIsListeningForCodesChanged();
}

void OnIsListeningForCodesChanged()
{
    if (IsListeningForCodes)
    {
        IsReadOnly = true;
        PrefixElement.Visibility = Visibility.Collapsed;
        Window.Current.CoreWindow.CharacterReceived += CoreWindow_CharacterReceived;
    }
    else
    {
        IsReadOnly = false;
        Focus(FocusState.Keyboard);
        PrefixElement.Visibility = Visibility.Visible;
        Window.Current.CoreWindow.CharacterReceived -= CoreWindow_CharacterReceived;
    }

    Text = string.Empty;
}