WPF:通过绑定获取对象(从转换器获取)属性

发布于 2024-11-02 23:22:39 字数 1904 浏览 3 评论 0原文

我遇到这种情况:我有一个枚举,我正在创建一个由基于该枚举的每个值的行组成的数据网格。

我将枚举值作为字符串传递给一些自定义转换器,并且工作正常。

然而,我遇到了一种情况,我希望使用转换器返回一个对象并绑定它的属性,而不是对象本身。 实际上我是通过转换器参数来做到这一点的,但问题是对于设计师来说这件事不起作用。

以下是一些代码片段:

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}}" />

可以做这样的事情吗?

I have this situation: I've an enum and I'm creating a datagrid composed of rows based on each value of this enum.

I'm passing enum values as strings to some customized converters and it's working fine.

However I reach a situation where I would like, with a converter, to return an object and bind it's property, not the object itself.
Actually I'm doing this through the converter parameter, but the problem is that with the designer this thing doesn't work.

Here are some pieces of code:

XAML:

<DataGridTextColumn Header="Comments" Binding="{Binding Converter={specializedconverters:ButtonToButtonMacroConverter}, ConverterParameter=Comments, Mode=OneWay}" Width="*" />

Converter:

[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();
    }
}

What I would like to know is if there is a better way to do this and see it at design time too.
I would like to return through the converter the object itself (ButtonMacro) and access it's property in the binding, something like Path=Comments (which is a property of ButtonMacro)

Example:

<DataGridTextColumn Header="Comments" Binding="{Binding Converter={specializedconverters:ButtonToButtonMacroConverter}, Path=Comments, Mode=OneWay}" Width="*" />

Is something like this possible?

Update 1:

<TextBlock Text="{Binding Source={Binding Converter={specializedconverters:ButtonToButtonMacroConverter}, Mode=OneWay}, Converter={converters:ObjectToStringConverter}}" />

Is possible to do something like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不醒的梦 2024-11-09 23:22:39

在这种情况下,我建议使用 DataGridTemplateColumn:

    <DataGrid AutoGenerateColumns="False" Height="200" Name="dataGrid1" Width="200">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Tag="{Binding Path=MyBrush, Converter={StaticResource myBrushConverter}}" 
                                       Text="{Binding RelativeSource={RelativeSource Self}, Path=Tag.Color}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

MyBrushConverter 返回 SolidColorBrush。 DataGridTextColumn 没有 Tag 属性。

In this case I would suggest to use DataGridTemplateColumn:

    <DataGrid AutoGenerateColumns="False" Height="200" Name="dataGrid1" Width="200">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Tag="{Binding Path=MyBrush, Converter={StaticResource myBrushConverter}}" 
                                       Text="{Binding RelativeSource={RelativeSource Self}, Path=Tag.Color}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

MyBrushConverter returns SolidColorBrush. DataGridTextColumn has no Tag property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文