2
我有這種情況:我有一個枚舉,我創建一個基於此枚舉的每個值組成的數據網格。WPF:通過綁定獲取對象(從轉換器獲得)屬性
我將枚舉值作爲字符串傳遞給一些自定義的轉換器,它工作正常。
但是我遇到了一種情況,我想用轉換器返回一個對象並綁定它的屬性,而不是對象本身。 其實我通過轉換器參數來做這件事,但問題是,與設計器這個東西不起作用。
這裏有一些代碼段:
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)和訪問返回它在綁定,像路徑=評論(這是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}}" />
可以做這樣的事情?
我很快就在測試它,謝謝 – 2011-04-20 11:59:27
它運行良好,但是我不得不使用Tag屬性,還有其他可能的方法嗎? (順便說一下,解決方案是可以的) – 2011-04-20 14:49:01