【问题标题】:WPF User control editable propertyWPF 用户控件可编辑属性
【发布时间】:2012-03-31 15:52:31
【问题描述】:

这是我在 Blend 中创建的 UserControl

<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 文本属性是可编辑的,这样我就可以在后面的 c# 代码中更改它。

怎么做?

【问题讨论】:

    标签: c# wpf xaml user-controls expression-blend


    【解决方案1】:

    只需使用 x:Name = "myTextBlock" 为 TextBlock 命名

    然后在后面的代码中你可以使用 myTextBlock.Text = "some other 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>
    

    【讨论】:

      【解决方案2】:

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

      <TextBlock Name="_textBox" ...
      

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

      【讨论】:

        猜你喜欢
        • 2014-08-01
        • 2011-01-06
        • 2018-04-23
        • 1970-01-01
        • 2011-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多