2012-03-31 59 views
2

这是我UserControl在Blend创建:WPF用户控件编辑属性

<StackPanel Orientation="Horizontal" Background="#FF0084FF" Height="400"> 
     <TextBlock TextWrapping="Wrap" Margin="10,0,20,0" Text="Kategorija1" FontFamily="Segoe Print" FontWeight="Bold" FontSize="26.667" RenderTransformOrigin="0.5,0.5" Foreground="White" TextAlignment="Center" VerticalAlignment="Center"> 
      <TextBlock.RenderTransform> 
       <TransformGroup> 
        <ScaleTransform/> 
        <SkewTransform/> 
        <RotateTransform Angle="-90"/> 
        <TranslateTransform/> 
       </TransformGroup> 
      </TextBlock.RenderTransform> 
     </TextBlock> 

现在我想TextBlock text属性可编辑,这样我就可以在C#代码改变它的后面。

如何做到这一点?

回答

4

只需使用x给TextBlock的名称:NAME =“myTextBlock”

然后在后面的代码,你可以使用myTextBlock.Text =“其他一些文字”

<StackPanel Orientation="Horizontal" Background="#FF0084FF" Height="400"> 
    <TextBlock x:Name = "myTextBlock" TextWrapping="Wrap" Margin="10,0,20,0" Text="Kategorija1" FontFamily="Segoe Print" FontWeight="Bold" FontSize="26.667" RenderTransformOrigin="0.5,0.5" Foreground="White" TextAlignment="Center" VerticalAlignment="Center"> 
     <TextBlock.RenderTransform> 
      <TransformGroup> 
       <ScaleTransform/> 
       <SkewTransform/> 
       <RotateTransform Angle="-90"/> 
       <TranslateTransform/> 
      </TransformGroup> 
     </TextBlock.RenderTransform> 
    </TextBlock> 
</StackPanel> 

如果你需要修改它的类之外,你可以使用x:FieldModifier把它公开,以及使任何外班可以修改它。

<StackPanel Orientation="Horizontal" Background="#FF0084FF" Height="400"> 
    <TextBlock x:Name = "myTextBlock" x:FieldModifier="public" TextWrapping="Wrap" Margin="10,0,20,0" Text="Kategorija1" FontFamily="Segoe Print" FontWeight="Bold" FontSize="26.667" RenderTransformOrigin="0.5,0.5" Foreground="White" TextAlignment="Center" VerticalAlignment="Center"> 
     <TextBlock.RenderTransform> 
      <TransformGroup> 
       <ScaleTransform/> 
       <SkewTransform/> 
       <RotateTransform Angle="-90"/> 
       <TranslateTransform/> 
      </TransformGroup> 
     </TextBlock.RenderTransform> 
    </TextBlock> 
</StackPanel> 
+0

+1 for x:FieldModifier。我只是不知道。 – 2012-03-31 18:10:12

2

为了在后面的代码中编辑TextBlock,您需要给它一个名称,通过它可以访问它。

<TextBlock Name="_textBox" ... 

现在后面的代码中,你可以通过名称来访问它_textBox