【发布时间】:2011-08-09 03:26:38
【问题描述】:
我有这种情况:我有一个枚举,我正在创建一个由基于该枚举的每个值的行组成的数据网格。
我将枚举值作为字符串传递给一些自定义转换器,它工作正常。
但是我遇到了一种情况,我希望使用转换器返回一个对象并绑定它的属性,而不是对象本身。 其实我是通过转换器参数来做这个的,但是问题是设计师这个东西是行不通的。
下面是一些代码:
XAML:
<DataGridTextColumn Header="Comments" Binding="{Binding Converter={specializedconverters:ButtonToButtonMacroConverter}, ConverterParameter=Comments, Mode=OneWay}" Width="*" />
转换器:
[ValueConversion(typeof(string), typeof(object))]
internal class ButtonToButtonMacroConverter : BaseConverter, IValueConverter
{
public ButtonToButtonMacroConverter() { }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool)DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)
return "Data visible only at runtime";
ButtonMacro macro = CurrentProfile.Profile.GetMacro((Buttons)Enum.Parse(typeof(Buttons), value as string));
return macro.GetType().GetProperty(parameter as string).GetValue(macro, null);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
我想知道是否有更好的方法来做到这一点并在设计时也看到它。 我想通过转换器返回对象本身(ButtonMacro)并在绑定中访问它的属性,例如 Path=Comments(这是 ButtonMacro 的属性)
例子:
<DataGridTextColumn Header="Comments" Binding="{Binding Converter={specializedconverters:ButtonToButtonMacroConverter}, Path=Comments, Mode=OneWay}" Width="*" />
这样的事情可能吗?
更新 1:
<TextBlock Text="{Binding Source={Binding Converter={specializedconverters:ButtonToButtonMacroConverter}, Mode=OneWay}, Converter={converters:ObjectToStringConverter}}" />
有可能做这样的事情吗?
【问题讨论】:
标签: c# wpf xaml binding converter