简体   繁体   English

创建分层的自定义控件模板(WPF 4 / .net 4.0 / C#)

[英]Creating Layered Custom Control Templates (WPF 4 / .net 4.0 / C#)

I need some help in trying to recreate the following style: 在尝试重新创建以下样式时,我需要一些帮助:

我的自定义样式

The sea of red can be ignored because it's just the background the textbox is sitting on. 可以忽略红色海洋,因为它只是文本框所在的背景。

To create the textbox I use the following xaml: 要创建文本框,我使用以下xaml:

            <TextBox Name="tbSorageName"
                     Grid.Column="1"
                     Width="250"
                     Height="30"
                     Margin="0"
                     HorizontalAlignment="Left"
                     VerticalAlignment="Top"
                     IsReadOnly="True"
                     Style="{StaticResource MainTextBoxStyle}"
                     Text="{Binding SelectedStorage.Name,
                                    Mode=OneWay,
                                    UpdateSourceTrigger=PropertyChanged}" />

To style it, I used the following style: 要设置样式,我使用了以下样式:

<Style x:Key="MainTextBoxStyle" TargetType="TextBox">
    <Setter Property="FontSize" Value="16" />
    <Setter Property="Foreground" Value="Snow" />
    <Setter Property="FontFamily" Value="Calibri" />
    <Setter Property="TextAlignment" Value="Center" />
    <Setter Property="Background" Value="{DynamicResource MainTextBox_BGBrush}" />
    <Setter Property="BorderBrush" Value="{DynamicResource MainTextBox_BorderBrush}" />
    <Setter Property="BorderThickness" Value="2" />
    <Setter Property="TextAlignment" Value="Left" />
</Style>

The Brushes I use are: 我使用的画笔是:

<SolidColorBrush x:Key="MainTextBox_BGBrush" Color="#3A3A3A" />
<SolidColorBrush x:Key="MainTextBox_BorderBrush" Color="#656565" />

That creates the basic textbox I use in my application, but I want to take my design much further by floating some meaningful text that describes the contents of textbox on the right hand side of the text box - So it should be anchored to the right hand side. 这样就创建了我在应用程序中使用的基本文本框,但是我想通过在文本框的右侧浮动一些描述文本框内容的有意义的文本来进一步设计,所以应该将其锚定在右侧侧。

You can do this easily via 2 approaches. 您可以通过2种方法轻松完成此操作。

1.Create a user control that consists of a textbox and a textblock. 1.创建一个由文本框和文本块组成的用户控件。 The Xaml for the UserControl would look similar to the following UserControl的Xaml类似于以下内容

<Grid Width="{Binding Path=Width, ElementName=tbSorageName}" Height="{Binding Path=Height, ElementName=tbSorageName}">
   <TextBox Name="tbSorageName"
                 Grid.Column="1"
                 Width="250"
                 Height="30"
                 Margin="0"
                 HorizontalAlignment="Left"
                 VerticalAlignment="Top"
                 IsReadOnly="True"
                 Style="{StaticResource MainTextBoxStyle}"
                 Text="hello" />
    <TextBlock HorizontalAlignment="Right" Margin="5" Foreground="Red" FontStyle="Italic">Name</TextBlock>
</Grid>

2.Overlay an Adorner on the textbox and draw the description over the Textbox yourself via an OnRender Override. 2.在文本框上覆盖一个Adorner ,并通过OnRender Override自己在文本框上绘制描述。

I would go with the Usercontrol approach since that is cleaner and easier to maintain. 我会使用Usercontrol方法,因为它更干净且易于维护。

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

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