在运行时

时间:2017-08-26 16:43:26

标签: c# wpf

我需要在运行时更改TextBlock x:name =“TxtRect”的文本,该文本来自样式资源文件。我正在尝试在代码中获取TextBlock元素并在运行时编辑其值。然后按我的代码获取和更改文本,但在运行项目时不会改变。

这是我的usercontrol1.xaml

     <Grid>            

            <Button Name="Btn1" Content="btn1"
                ToolTip="{Binding DisplayName}"/>


            <Button  Name="Btn2" Content="btn2"
                ToolTip="{Binding DisplayName}" />

     </Grid>

这是我在Styles.xaml中的资源

    <Style x:Key="TileButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="BorderBrush" Value="#FF363636"/>
        <Setter Property="Height" Value="170"/>
        <Setter Property="Width" Value="235"/>
        <Setter Property="Margin" Value="5"/>
        <Setter Property="BorderThickness" Value="5"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="LayoutGrid">
                        <Grid.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </Grid.RenderTransform>
                        <Rectangle x:Name="rectangle" Fill="{TemplateBinding Background}" 
                                   Stroke="{TemplateBinding BorderBrush}" 
                                   StrokeThickness="{TemplateBinding 
                        BorderThickness}"/>
                        <TextBlock x:Name="TxtRect" Text="Hi there" 
                                   HorizontalAlignment="Left"
                                   VerticalAlignment="Bottom" 
                                   TextWrapping="Wrap"
                                   Foreground="#FFF"
                                   Margin="5"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
     </Style>

这里我有我的usercontrol1.cs

    public UserControl1()
    {
        InitializeComponent();

        var myStyle = TryFindResource("TileButtonStyle") as Style;

         if (myStyle != null)
        {
            foreach (var s in myStyle.Setters)
            {
                if (((Setter)s).Property.ToString().Equals("Template"))
                {
                    var franworkTemplate = (FrameworkTemplate)((Setter)s).Value;
                    var txtBlock = franworkTemplate.LoadContent().FindChildren<TextBlock>().FirstOrDefault(o => o.Name == "TxtRect");

                    if (txtBlock == null) continue;
                    txtBlock.SetValue(TextBlock.TextProperty, "dsafasfs"); //this is not working at all
                    txtBlock.Text = "NewText"; // this works but in debuging mode and not change text value in runtime for view xaml page
                }
            }

            Btn1.SetValue(Control.StyleProperty, myStyle);
            Btn2.SetValue(Control.StyleProperty, myStyle);
        }
    }

这是我正在使用的实际按钮:

   <DataTemplate  DataType="{x:Type}">
            <Button Style="{DynamicResource TileButtonStyle}" Content="{StaticResource UserFigure}" x:Name="Btn1"
                    ToolTip="{Binding DisplayName}" Command="{Binding DataContext.GoToCommand, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" CommandParameter="{Binding RegisteredName}"/>
        </DataTemplate>

1 个答案:

答案 0 :(得分:-1)

这是因为直接分配的Content具有比样式设置器更高的优先级,因此Content将覆盖setter文本后面的代码。

修改 Actualy这个更复杂,但是你可以用下面的代码来实现这个目标

而不是这个,您可以使用模板binidng到按钮的Content属性,如下所示:

<TextBlock x:Name="TxtRect" Text="{TemplateBinding Content}" 
                                   HorizontalAlignment="Left"
                                   VerticalAlignment="Bottom" 
                                   TextWrapping="Wrap"
                                   Foreground="#FFF"
                                   Margin="5"/>

然后你只需在运行时更改它:

Btn1.Content = "Hi there"