WPF:将路径应用于TextBox模板

时间:2014-11-21 16:08:51

标签: c# wpf xaml textbox

在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}" />    

结果如下:

Speech Bubble Text Box

1 个答案:

答案 0 :(得分:1)

如果是我,我只是把它放进一个像<; p>这样的模板中

<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"/>

您必须稍微调整一下才能使其显示出您想要的效果,但您明白了。希望这会有所帮助,欢呼。

P.S。 - 如果你想把它放在TextBox模板中,或者也很容易。