wpf 如何在用户控件中添加画布 xaml 资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18657162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:36:09  来源:igfitidea点击:

How to add canvas xaml resource in usercontrol

c#wpfxaml

提问by Robby Smet

I have downloaded this pack : http://modernuiicons.com/and I'm trying to use the xaml icons.

我已经下载了这个包:http: //modernuiicons.com/并且我正在尝试使用 xaml 图标。

I have added a xaml file to my solution with the following content

我在我的解决方案中添加了一个 xaml 文件,内容如下

<?xml version="1.0" encoding="utf-8"?>
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="appbar_check" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
<Path Width="37.9998" Height="31.6665" Canvas.Left="19.0001" Canvas.Top="22.1668" Stretch="Fill" Fill="#FF000000" Data="F1 M 23.7501,33.25L 34.8334,44.3333L 52.2499,22.1668L 56.9999,26.9168L 34.8334,53.8333L 19.0001,38L 23.7501,33.25 Z "/>
</Canvas>

Now, how do I reference this canvas to my usercontrol?

现在,我如何将此画布引用到我的用户控件?

Usercontrol

用户控件

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
mc:Ignorable="d"
x:Class="UserControlSolution.UserControlButton"
x:Name="UserControl"
Height="50" Background="#FF2F2F2F" BorderBrush="#FF919191">


<Grid x:Name="LayoutRoot" Height="50" RenderTransformOrigin="0.5,0.5">
    <Rectangle x:Name="rectangle" RenderTransformOrigin="0.5,0.5" Width="230" Height="50"/>
    <TextBlock x:Name="NameLabel" FontSize="16" Foreground="#FFE5E5E5" Height="34" Width="149" Text="Onthaal Telefoon" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,10,0,0" FontFamily="Segoe UI Semibold"/>
    <Viewbox HorizontalAlignment="Right" VerticalAlignment="Top" Height="16.5" Width="17.789" Margin="0,15,24.5,0">
        // Here I want to reference the canvas
    </Viewbox>
</Grid>
</UserControl>

I can copy the content of the canvas offcourse but there must be another solution.

我可以在课外复制画布的内容,但必须有另一种解决方案。

回答by Luke Puplett

Add the Canvasand Pathas a resource on the page or in the App.xaml or whatever, remember to set x:Key. Then use a ContentControlto reference the resource.

在页面或 App.xaml 或其他任何内容中添加CanvasPath作为资源,记得设置x:Key. 然后使用 aContentControl来引用资源。

<!-- In Resources, either on the Page or App.xaml for app-wide reuse -->
<Canvas x:Key="TickCanvas" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="appbar_check" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
    <Path Width="37.9998" Height="31.6665" Canvas.Left="19.0001" Canvas.Top="22.1668" Stretch="Fill" Fill="#FF000000" Data="F1 M 23.7501,33.25L 34.8334,44.3333L 52.2499,22.1668L 56.9999,26.9168L 34.8334,53.8333L 19.0001,38L 23.7501,33.25 Z "/>
</Canvas

<!-- On your page, or somewhere -->
<ViewBox>
    <ContentControl Content="{StaticResource TickCanvas}" />
</ViewBox>

As proof it works, I was able to see its a tick!

作为证明它有效,我能够看到它的一个勾号!

Just a side note, I often take just the path data, the mini-markup and save that as a string resource. Then using a PathI reference the markup resource via Data={StaticResource TickPath}that way I can resize the vector using the Heightand Widthon the Pathitself or let it expand and shrink by its parent by setting Stretch="Uniform". Saves the overhead of the Viewbox.

顺便提一下,我经常只获取路径数据、迷你标记并将其保存为字符串资源。然后使用PathI 通过Data={StaticResource TickPath}这种方式引用标记资源,我可以使用HeightWidth在其Path自身上调整向量的大小,或者通过设置Stretch="Uniform". 节省了Viewbox.

<!-- In App.xaml for app-wide reuse -->
<x:String x:Key="TickPath">F1 M 23.7501,33.25L 34.8334,44.3333L 52.2499,22.1668L 56.9999,26.9168L 34.8334,53.8333L 19.0001,38L 23.7501,33.25 Z </x:String>

<!-- On page, template or wherever -->
<Path Data="{StaticResource TickPath} />

This technique may not work in this instance as there's a clip geometry there. But for simple vectors its fine, I have hand drawn typefaces (that can't be embedded as fonts) stored as markup in files, I then load them in at runtime - Data={Binding PathData}works just as well.

这种技术在这种情况下可能不起作用,因为那里有一个剪辑几何体。但是对于简单的矢量它很好,我将手绘字体(不能作为字体嵌入)作为标记存储在文件中,然后我在运行时加载它们 -Data={Binding PathData}工作得一样好。

回答by jv_

A variation based on Luke's answer allowing a color to be specified down to the path.

基于 Luke 的答案的变体,允许指定路径的颜色。

<Style TargetType="{x:Type ContentControl}" x:Key="TickIcon">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Viewbox Width="{Binding Width, RelativeSource={RelativeSource AncestorType=ContentControl}}">
                    <Canvas x:Name="appbar_check" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
                        <Path Width="37.9998" Height="31.6665" Canvas.Left="19.0001" Canvas.Top="22.1668" Stretch="Fill" Fill="{Binding Foreground, RelativeSource={RelativeSource AncestorType=ContentControl}}" Data="F1 M 23.7501,33.25L 34.8334,44.3333L 52.2499,22.1668L 56.9999,26.9168L 34.8334,53.8333L 19.0001,38L 23.7501,33.25 Z "/>
                    </Canvas>
                </Viewbox>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

and

<Button Command="{Binding ConnectCommand}" VerticalAlignment="Stretch">
    <WrapPanel>
        <ContentControl Foreground="AliceBlue" Style="{StaticResource TickIcon}" Width="20" />
        <TextBlock>Connect</TextBlock>
    </WrapPanel>
</Button>