【问题标题】:Changing button content with code behind使用后面的代码更改按钮内容
【发布时间】:2014-07-02 20:50:03
【问题描述】:

这是我的代码:

Button obj = sender as Button;
        obj.Content = "fgfgfgfghhhhhhhhhhhhhpetko";
        obj.Foreground = Brushes.White;
        obj.Height = 20;

它在 MouseEntered 事件中。当我将鼠标悬停在按钮上时,按钮会更改其高度,但不会更改其内容。文本保持不变。为什么会这样?

这是按钮样式:

<Style x:Key="RoundCorner" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid x:Name="grid" >
                    <Border x:Name="border" CornerRadius="8">
                        <Border.Background>
                            SlateBlue
                        </Border.Background>
                    </Border>
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" >
                        <ContentPresenter.Content>
                            <StackPanel Orientation="Horizontal">
                                <Image Source="/Resources/test.png"></Image>
                                <TextBlock HorizontalAlignment="Right">
                                    <TextBlock.Name>
                                        petko
                                    </TextBlock.Name>
                                </TextBlock>
                            </StackPanel>
                        </ContentPresenter.Content>
                    </ContentPresenter>

                </Grid>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我是这样使用它的:

<Button  MouseEnter="MouseEntered" Style="{StaticResource RoundCorner}" Name="btnHelp" Command="NavigationCommands.GoToPage" CommandParameter="ViewModel/MyPage.xaml" CommandTarget="{Binding ElementName=frmContent}" ></Button>

【问题讨论】:

  • 它对我来说很好用。按钮内容已更新,能否请您实际编写代码
  • @Firoz 我更新了问题。请看
  • 您确定您已将事件处理程序添加到按钮吗?我在您的 xaml 中没有看到处理程序规范
  • @Teaman 是的,我确定。我再次更新了问题。
  • @petko_stankoski 您要更改TextBlock 文本还是替换整个Content(包括Image)?

标签: wpf button


【解决方案1】:

当我使用您的模板时,为按钮设置任何内容对我不起作用(即使在 XAML 文件中硬编码)。我已经稍微修改了您的模板以不覆盖ControlTemplate 内的ContentContainer.Content,它现在似乎可以正常工作并且在鼠标输入事件时更新。

<Style x:Key="RoundCorner" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="grid" >
                        <Border x:Name="border" CornerRadius="8">
                            <Border.Background>
                                SlateBlue
                            </Border.Background>
                        </Border>
                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
                            <Image Source="/Resources/test.png"></Image>
                            <ContentPresenter />
                        </StackPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

如果这能解决您的问题,请告诉我。

【讨论】:

【解决方案2】:

你已经硬编码了模板,你必须使用模板绑定来更新它。

<Style x:Key="RoundCorner" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid x:Name="grid" >
                    <Border x:Name="border" CornerRadius="8">
                        <Border.Background>
                            SlateBlue
                        </Border.Background>
                    </Border>
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" >
                        <ContentPresenter.Content>
                            <StackPanel Orientation="Horizontal">
                                <Image Source="/Resources/test.png"></Image>
                                     <TextBlock HorizontalAlignment="Right" Text="{TemplateBinding Content}">
                               </StackPanel>
                        </ContentPresenter.Content>
                    </ContentPresenter>

                </Grid>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

【讨论】:

【解决方案3】:

问题是您在模板内设置ContentPresenter.Content。此属性绑定到按钮的实际内容。您应该为按钮本身而不是模板设置此内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多