简体   繁体   English

在 WPF XAML 中绑定和渲染 Visual 属性

[英]Bind and render Visual property in WPF XAML

I think I am struggling with something really basic, but I cannot find a way to bind and render a 'Visual' type property.我想我正在为一些非常基本的东西而苦苦挣扎,但我找不到绑定和呈现“视觉”类型属性的方法。

In my model, I have a list of user names, along with a Identicon rendered by this nice library.在我的 model 中,我有一个用户名列表,以及由这个漂亮的库呈现的 Identicon。

https://jdenticon.com/net-api/M_Jdenticon_WpfExtensions_ToVisual.html https://jdenticon.com/net-api/M_Jdenticon_WpfExtensions_ToVisual.html

For WPF it provides a.ToVisual() extension method, however I don't know how to make that 'Visual' appear in the UI.对于 WPF 它提供了一个 .ToVisual() 扩展方法,但是我不知道如何使该“视觉”出现在 UI 中。 I tried several approaches such as Rectangle.Fill, Paths, ContentPresenters etc.我尝试了几种方法,例如 Rectangle.Fill、Paths、ContentPresenters 等。

In general, the binding looks as follows - the UserIcon is a property of type Visual.通常,绑定如下所示 - UserIcon 是 Visual 类型的属性。

 <CheckBox.Content>
                                    <Grid Margin="0,2" >
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="16" />
                                            <ColumnDefinition Width="120" />
                                        </Grid.ColumnDefinitions>
                                        <Image Grid.Column="0" Source="{Binding User.UserIcon}"></Image>
                                        <TextBlock Grid.Column="1" Text="{Binding User.Name}" Margin="3,0"/>
                                    </Grid>
                                </CheckBox.Content>

How do I render this?我该如何渲染这个?

You may use a VisualBrush:您可以使用 VisualBrush:

<Rectangle Width="100" Height="100">
    <Rectangle.Fill>
        <VisualBrush Visual="{Binding User.UserIcon}"/>
    </Rectangle.Fill>
</Rectangle>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM