【问题标题】:WPF: Apply Path to TextBox TemplateWPF:将路径应用于文本框模板
【发布时间】:2014-11-22 02:58:22
【问题描述】:

在 Blend 中,我创建了一个看起来像语音气泡的路径。我想将此路径用作 TextBox 或 TextBlock 的样式或模板(我不在乎,因为这将始终用于只读文本)。如何将路径应用到 TextBox 或 TextBlock 以创建自己的样式或模板?

XAML 路径:

<Path Data="M27.499998,2.5 L218,2.5 C231.80711,2.5000005 243,13.692882 243,27.500002 L243,114.5 C243,128.30711 231.80711,139.5 218,139.5 L176.86046,139.5 176.77534,140.60143 C173.04614,179.27731 126.53165,183.52524 126.53165,183.52524 143.526,165.98947 145.27682,147.30386 145.22961,139.96802 L145.222,139.5 27.499998,139.5 C13.692882,139.5 2.5,128.30711 2.5,114.5 L2.5,27.500002 C2.5,13.692882 13.692882,2.5000005 27.499998,2.5 z" 
      Fill="#FF5E9E5B" 
      HorizontalAlignment="Left" 
      Height="186.025" 
      Stretch="Fill" 
      Stroke="Black" 
      StrokeThickness="5" 
      VerticalAlignment="Top" 
      Width="245.5" />

解决方案:(感谢 Chris W. 的回答)

如果有人遇到这种情况,下面是我如何实施解决方案来创建一个自动调整内容大小的文本框语音气泡。

在 StylesDictionary.xaml 中:

<Style x:Key="SpeechBubbleTextBoxStyle" TargetType="{x:Type ContentControl}">
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="VerticalContentAlignment" Value="Top" />
    <Setter Property="Margin" Value="10" />
    <!-- Add additional Setters Here -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ContentControl}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBox Text="{TemplateBinding Content}" 
                             VerticalAlignment="Top" 
                             HorizontalAlignment="Stretch"
                             Padding="10"
                             TextWrapping="Wrap"
                             BorderBrush="Black" 
                             BorderThickness="2" 
                             Background="#FF5E9E5B"
                             Grid.Row="0">
                        <TextBox.Resources>
                            <Style TargetType="{x:Type Border}">
                                <Setter Property="CornerRadius" Value="15" />
                                <Setter Property="BorderBrush" Value="Black" />
                                <Setter Property="BorderThickness" Value="3" />                                            
                            </Style>
                        </TextBox.Resources>
                    </TextBox>
                    <Path Data="M332,212.5 C332,250.00012 294.00041,254.50014 294.00041,254.50014 312.91688,228.75005 308.39508,212.54091 308.39508,212.54091" 
                          Fill="#FF5E9E5B" 
                          HorizontalAlignment="right" 
                          VerticalAlignment="Top" 
                          Height="34" 
                          Width="43" 
                          Margin="0,-3,50,0"
                          Stretch="Fill" 
                          Stroke="Black" 
                          StrokeThickness="2" 
                          UseLayoutRounding="True"
                          Grid.Row="1"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后在我的 UserControl 中我这样称呼它:

    <ContentControl Style="{StaticResource SpeechBubbleTextBoxStyle}"
                    Content="{Binding InstructionsText}" />    

结果如下:

【问题讨论】:

  • 您不能为TextBlock 创建模板,但您可以将TextBlock 放在Path 之上,例如将两者放在Grid

标签: c# wpf xaml textbox


【解决方案1】:

如果是我,我会把它扔进一个模板中;

<Window.Resources>
    <Style x:Key="ChatBubbleThingy" TargetType="{x:Type ContentControl}">
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="10"/>
            <!-- Add additional Setters Here -->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContentControl}">
                    <Grid>
                        <Path Data="M27.499998,2.5 L218,2.5 C231.80711,2.5000005 243,13.692882 243,27.500002 L243,114.5 C243,128.30711 231.80711,139.5 218,139.5 L176.86046,139.5 176.77534,140.60143 C173.04614,179.27731 126.53165,183.52524 126.53165,183.52524 143.526,165.98947 145.27682,147.30386 145.22961,139.96802 L145.222,139.5 27.499998,139.5 C13.692882,139.5 2.5,128.30711 2.5,114.5 L2.5,27.500002 C2.5,13.692882 13.692882,2.5000005 27.499998,2.5 z" 
                              Fill="#FF5E9E5B"
                              Height="186.025" 
                              Stretch="Fill" 
                              Stroke="Black" 
                              StrokeThickness="5"
                              Width="245.5" />
                        <TextBlock Text="{TemplateBinding Content}" 
                                   Margin="{TemplateBinding Padding}" 
                                   VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                   HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                    </Grid>                             
                </ControlTemplate>
            </Setter.Value>
        </Setter>                      
    </Style>
</Window.Resources>

然后像这样调用它;

<ContentControl Style="{StaticResource ChatBubbleThingy}" 
                Content="Blah Blah Blah Blah"/>

您必须对其进行一些调整才能使其完全按照您想要的方式显示,但您明白了。希望这会有所帮助,干杯。

附: - 如果您想将其放入 TextBox 模板或其他简单的东西中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    相关资源
    最近更新 更多