简体   繁体   English

C#WPF-创建自定义控件。 不确定如何正确对齐文本

[英]C# WPF - Creating Custom Control. Not sure how to align Text Correctly

I'm trying to create a custom control; 我正在尝试创建一个自定义控件; similar, say to a Compass, so it is circular. 类似于指南针,所以它是圆形的。 I have a compass face image, and an arrow image that I can rotate to point the right direction. 我有一个指南针脸图像和一个箭头图像,可以旋转以指向正确的方向。 The problem is that I have N,S,E,W markers that I want to update at times and thus made them labels. 问题是我有N,S,E,W个标记,我想不时更新并因此使其成为标签。 I divided the field up into percentages and put the labels in the right grid locations. 我将字段分成多个百分比,然后将标签放在正确的网格位置。

This all looks great. 这一切看起来都很棒。

BUT when I use this custom control on various pages, some work and some don't. 但是,当我在各种页面上使用此自定义控件时,有些工作有用,有些则不用。 What it comes down to is the size of the cell of the grid on the new page. 归结为新页面上的网格单元格的大小。 In one case the grid width is 4x the grid height. 在一种情况下,栅格宽度是栅格高度的4倍。 The background image grows to fill the height; 背景图像会增长到填满高度; this is right. 这是对的。 The West and East TextBlocks are way off of the Compass_Face image because the 1/4th the width of the cell is way to the left. 西方和东方文字块与Compass_Face图像相去甚远,因为该单元格宽度的1/4位于左边。 I thought that they would be constrained to the size of the custom control but that appears to not be the problem. 我认为它们将受限于自定义控件的大小,但这似乎不是问题。

I'm sure that there has to be a way to keep the text constrained to the compass_face image. 我敢肯定,必须有一种方法可以将文本限制在指南针图像中。 Thoughts? 有什么想法吗?

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="25*"/>
        <ColumnDefinition Width="50*"/>
        <ColumnDefinition Width="25*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="25*"/>
        <RowDefinition Height="50*"/>
        <RowDefinition Height="25*"/>
    </Grid.RowDefinitions>
    <Image x:Name="imageBackground" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" Grid.RowSpan="3" Source="Resources/compass_face.png"/>
    <Image x:Name="imageArrow" Grid.Column="1" Grid.Row="1" Source="Resources/arrow.png"/>
    <TextBlock x:Name="labelNorth" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontWeight="Bold" Text="N"/>
    <TextBlock x:Name="labelSouth" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontWeight="Bold" Text="S"/>
    <TextBlock x:Name="labelEast" Grid.Column="2" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontWeight="Bold" Text="E"/>
    <TextBlock x:Name="labelWest" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontWeight="Bold" Text="W"/>
</Grid>

Grid isn't really the best layout to use in this case, but you can force it to be square by putting a Viewbox around it and assigning your parent grid to a specific size. 在这种情况下,网格实际上并不是最好的布局,但是您可以通过在其周围放置一个Viewbox并将其父网格分配给特定的大小来强制其为正方形。 That will keep your text aligned properly: 这样可以使文本正确对齐:

<Viewbox>
    <Grid Width="200" Height="200">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="25*"/>
            <ColumnDefinition Width="50*"/>
            <ColumnDefinition Width="25*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="25*"/>
            <RowDefinition Height="50*"/>
            <RowDefinition Height="25*"/>
        </Grid.RowDefinitions>

        <!--<Image x:Name="imageBackground" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" Grid.RowSpan="3" Source="Resources/compass_face.png"/>
        <Image x:Name="imageArrow" Grid.Column="1" Grid.Row="1" Source="Resources/arrow.png"/>-->
        <Ellipse Fill="CornflowerBlue" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" Grid.RowSpan="3" />
        <TextBlock Grid.Column="1" Grid.Row="1" Text="&#x2191;" FontSize="200" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" />

        <TextBlock x:Name="labelNorth" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontWeight="Bold" Text="N"/>
        <TextBlock x:Name="labelSouth" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontWeight="Bold" Text="S"/>
        <TextBlock x:Name="labelEast" Grid.Column="2" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontWeight="Bold" Text="E"/>
        <TextBlock x:Name="labelWest" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontWeight="Bold" Text="W"/>
    </Grid>
</Viewbox>

在此处输入图片说明

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

相关问题 如何遍历treeView控件的所有节点。 C# - How to Iterate through all nodes of a treeView Control. C# 创建分层的自定义控件模板(WPF 4 / .net 4.0 / C#) - Creating Layered Custom Control Templates (WPF 4 / .net 4.0 / C#) c#“此方法或属性不可用,因为当前选择部分覆盖了纯文本内容控件。” - c# “this method or property is not available because the current selection partially covers a plain text content control.” 如何在WPF C#中将按钮文本对齐到中心? - How do I align button text to center in WPF C#? 如何在自定义WPF C#项目内的AvalonEdit控件中加载大文本文件 - How to load large text files in AvalonEdit control inside custom WPF C# project 如何在WPF中使控件与单选按钮的文本对齐 - How To Align a Control to the Text of a Radio Button in WPF 如何添加Combobox作为LabelMenu项目进行标签控制。 WPF应用程序 - How to add Combobox as ContextMenu Item for Label Control. WPF app 删除网站的一部分; Web浏览器控件。 C# - Remove a section of a website; web browser control. C# 如何通过C#代码在自定义WPF控件中更改BitmapEffect - How to change BitmapEffect in custom WPF control via C# code C# wpf - 如何在自定义控件中将文本框错误传递给父级 - C# wpf - How to pass textbox error to parent in custom control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM