【发布时间】: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)?