简体   繁体   English

如何从代码隐藏更改XAML elemen't模板?

[英]How to change XAML elemen't template from code-behind?

I have next button's style defined in resources: 我在资源中定义了下一个按钮的样式:

<Style x:Key="OKBtn" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Rectangle .../>

                    <TextBlock x:Name="Text" ..>
                        <Run Language="en-en" Text="OK"/>
                    </TextBlock>                        
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And I want in some specified case from code change Button's text. 我希望在代码更改Button的文本的某些特定情况下。

Ie change "OK" ( <Run Language="en-en" Text="OK"/> ) to "Accept". 即改为“OK”( <Run Language="en-en" Text="OK"/> )为“Accept”。 How can I do that? 我怎样才能做到这一点?

Is it possible to access this TextBlock "Text" and change content exactly for my one button, but not for all OK buttons? 是否可以访问此TextBlock“文本”并更改我的一个按钮的内容,但不是所有的OK按钮?

My button: 我的按钮:

<Button x:Name="OkButton" Style="{DynamicResource OKBtn}" />

You can borrow some props from template Template , for example Tag property. 您可以从模板Template借用一些道具,例如Tag属性。 So the TextBlock text in the ControlTemplate should be like this. 所以ControlTemplateTextBlock文本应该是这样的。

<Run Language="en-en" Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Tag}"/>

And you can change the button caption by setting it's Tag property. 您可以通过设置Tag属性来更改按钮标题。

OkButton.Tag = "Accept";

And for not set all button texts manually you can create some ValueConverter to set TextBlock text in the ControlTemplate to the "Ok" whenever Tag property is empty. 并且,如果未手动设置所有按钮文本,则可以创建一些ValueConverter以便在Tag属性为空时将ControlTemplate中的TextBlock文本设置为“Ok”。

At first, you should declare ContentPresenter to show any object in your Content property of Button control. 首先,您应声明ContentPresenter以显示Button控件的Content属性中的任何对象。

<Style x:Key="OkBtn" TargetType="Button">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Rectangle/>
                    <ContentPresenter Content="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"></ContentPresenter>
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

Then, it is possible to set another Content by using code behind or binding : 然后,可以使用code behindbinding来设置另一个Content

By code behind: 通过代码落后:

okButton.Content="desirableText";

By binding: 通过绑定:

<Button x:Name="OkButton" Style="{DynamicResource OKBtn}" Content="{Binding FooText}" />

private string fooText;

public string FooText
{
   get { return fooText; }
   set
   {
       fooText = value;
       OnPropertyChanged("FooText");
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM