Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何画十字?_C#_.net_Wpf_Silverlight_Mvvm - Fatal编程技术网

C# 如何画十字?

C# 如何画十字?,c#,.net,wpf,silverlight,mvvm,C#,.net,Wpf,Silverlight,Mvvm,我有一个WPF控件 我需要在wpf控件中绘制如下内容。 调整控件大小时,十字是否应跟随其大小?我还必须按如下方式键入字母。我应该能够通过绑定到viewmodel属性以编程方式设置前景和背景 请参见本页关于如何绘制线条的介绍。我猜添加4个TextBlock对象可能会完成这项工作 你可以把整个东西放在一个格子里,这样缩放就很容易了。如果你用路径来画线/箭头,十字架本身就很简单了。使用缩放到容器大小: <Viewbox> <Grid Width="100" Height="1

我有一个WPF控件

我需要在wpf控件中绘制如下内容。 调整控件大小时,十字是否应跟随其大小?我还必须按如下方式键入字母。我应该能够通过绑定到viewmodel属性以编程方式设置前景和背景

请参见本页关于如何绘制线条的介绍。我猜添加4个TextBlock对象可能会完成这项工作


你可以把整个东西放在一个格子里,这样缩放就很容易了。

如果你用
路径
来画线/箭头,十字架本身就很简单了。使用缩放到容器大小:

<Viewbox>
    <Grid Width="100" Height="100">
        <TextBlock Text="N" HorizontalAlignment="Left" VerticalAlignment="Center" />
        <TextBlock Text="S" HorizontalAlignment="Right" VerticalAlignment="Center" />
        <TextBlock Text="E" VerticalAlignment="Top" HorizontalAlignment="Center" />
        <TextBlock Text="W" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
        <Path Stroke="Black" StrokeThickness="1"
                Data="M 15,50 L 85,50 M 80,45 L 85,50 M 80 55 L 85,50"
                />
        <Path Stroke="Black" StrokeThickness="1"
                Data="M 50,15 L 50,85 M 45,80 L 50,85 M 55 80 L 50,85"
                />
    </Grid>
</Viewbox>
现在,您可以像这样创建控件的实例(假设视图模型具有名为“前台”和“后台”的
Brush
类型属性):



您尝试过哪些不起作用的方法?看@Steve,我不知道怎么开始。我应该使用标签和控件模板或样式,还是应该使用多边形?嗯,McGarnagle,Billy死了!“这让我回过神来。”麦加纳格谢谢你的解决方案。它按预期工作。非常感谢
<UserControl x:Class="MyProject.Controls.Compass"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid x:Name="LayoutRoot" 
          Background="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Background}">
        <Viewbox>
            <Grid Width="100" Height="100">
                <TextBlock Text="N" HorizontalAlignment="Left" VerticalAlignment="Center" 
                           Foreground="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Foreground}" />
                <TextBlock Text="S" HorizontalAlignment="Right" VerticalAlignment="Center" 
                           Foreground="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Foreground}" />
                <TextBlock Text="E" VerticalAlignment="Top" HorizontalAlignment="Center" 
                           Foreground="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Foreground}" />
                <TextBlock Text="W" VerticalAlignment="Bottom" HorizontalAlignment="Center"
                           Foreground="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Foreground}" />
                <Path Stroke="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Foreground}" StrokeThickness="1"
                      Data="M 15,50 L 85,50 M 80,45 L 85,50 M 80 55 L 85,50" />
                <Path Stroke="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=Foreground}" StrokeThickness="1"
                      Data="M 50,15 L 50,85 M 45,80 L 50,85 M 55 80 L 50,85" />
            </Grid>
        </Viewbox>
    </Grid>
</UserControl>
<controls:Compass Foreground="{Binding Foreground}" Background="{Binding Background}"/>