IValueConverter.Convert不会在OneWay绑定上被调用

时间:2011-04-06 11:05:57

标签: c# silverlight data-binding

我有一个布尔属性(在setter中调用INotifyPropertyChanged),它绑定到我的XAML中的button.IsEnabled属性。目前我正在使用TwoWay绑定,但这会导致问题,我只需要OneWay绑定。我的问题是我正在使用的转换器在程序第一次启动时没有被调用。我在setter中放置了断点,它被称为load,但Convert()方法根本没有被调用。这是为什么?

一些代码:

public bool IsSaving
    {
        get
        {
            return _isSaving;
        }
        set
        {
            _isSaving = value;
            NotifyOfPropertyChange(() => IsSaving);
        }
    }

和XAML:

IsEnabled="{Binding Path=IsSaving, Mode=OneWay, Converter={StaticResource booleanToNotEnabledConverter}}"

转换器实际上只返回{​​{1}},因此当!(bool)value为真时,该按钮会被禁用。

3 个答案:

答案 0 :(得分:2)

运行时的某些更改可能会导致绑定中断(因为您绑定到DataContext +相对路径),如果使用Visual Studio,请确保检查Output-window是否存在任何绑定错误。

编辑:因为没有注意到:这是一个标准绑定,并且发布的代码没有任何问题,问题必须由上下文引起。

答案 1 :(得分:1)

以下是我使用的代码,这有效:

转换器:

using System.Windows.Data;
using System;
namespace SilverlightApplication1
{
    public class BooleanToNotEnabledConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return !(bool)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

XAML:

<UserControl x:Class="SilverlightApplication1.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:SilverlightApplication1"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="400">
    <UserControl.Resources>
        <local:BooleanToNotEnabledConverter x:Key="booleanToNotEnabledConverter" />
    </UserControl.Resources>
    <StackPanel Margin="100">

        <Button Content="Flip"
                Click="Button_Click" />
        <TextBlock Text="{Binding IsSaving}"
                   Height="20" />
        <Button IsEnabled="{Binding IsSaving, Mode=OneWay, Converter={StaticResource booleanToNotEnabledConverter}}"
                Content="Some Button" />
    </StackPanel>
</UserControl>

代码背后:

using System.Windows.Controls;
using System.Windows;
using System.ComponentModel;
namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        private Data _data;
        public MainPage()
        {
            InitializeComponent();
            _data = new Data { IsSaving = true };
            this.DataContext = _data;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _data.IsSaving = !_data.IsSaving;
        }
    }

    public class Data : INotifyPropertyChanged
    {
        #region IsSaving Property
        private bool _isSaving;
        public bool IsSaving
        {
            get
            {
                return _isSaving;
            }
            set
            {
                if (_isSaving != value)
                {
                    _isSaving = value;
                    OnPropertyChanged("IsSaving");
                }
            }
        }
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            var p = PropertyChanged;
            if (p != null)
            {
                p(this, new PropertyChangedEventArgs(propertyName));

            }
        }
    }
}

答案 2 :(得分:0)

您确定使用正确的字符串调用PropertyChanged事件处理程序吗?

PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsSaving"));