【发布时间】:2010-11-17 01:56:11
【问题描述】:
我正在根据 ListBox 中的 SelectedIndex >= 0 设置控件的 IsEnabled 属性。我可以在后面的代码中做到这一点,但我想为这种行为创建一个值转换器,因为这是我经常做的事情。
我创建了这个值转换器来处理任务并将它绑定到 IsEnabled 属性:
[ValueConversion(typeof(Selector), typeof(bool))]
public class SelectorItemSelectedToBooleanConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null || !(value is Selector))
return null;
var control = value as Selector;
return control.SelectedIndex >= 0;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
转换器仅在应用程序加载时调用一次。当 SelectedIndex 改变时它不会触发。
因此,我的问题是什么导致值转换器触发?我认为这是绑定数据发生变化的时候,那么有没有办法强制转换器在不同的情况下触发?我问对了问题吗?
【问题讨论】:
-
您能在 xaml 中显示您的绑定吗? SelectedIndex 是依赖属性,它应该可以工作
-
绑定不是到 SelectedIndex 属性,而是到 ListBox 本身。不过,我想您已经找到了答案:Control 本身不是 dp,因此只要内容发生更改, onyl 可能就会起作用。
标签: wpf data-binding converter