从c#隐藏代码更改按钮的内容
我的问题是关于wp7的。我试图在用户单击按钮后,更改c#behind代码中按钮的内容。特别是,我想更改位于网格(“GraphGrid”)内的三个路径元素的Fill属性。此网格是按钮本身的内容。 以下是关于按钮的XAML代码:从c#隐藏代码更改按钮的内容,c#,windows-phone-7,xaml,C#,Windows Phone 7,Xaml,我的问题是关于wp7的。我试图在用户单击按钮后,更改c#behind代码中按钮的内容。特别是,我想更改位于网格(“GraphGrid”)内的三个路径元素的Fill属性。此网格是按钮本身的内容。 以下是关于按钮的XAML代码: <Button.Content> <Grid x:Name="GraphGrid" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stre
<Button.Content>
<Grid x:Name="GraphGrid" Width="Auto" Height="Auto" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<Path x:Name="Path"
Data="M 0,0 0,80 20,80 20,0Z"
Stroke="Black"
Fill="Black"
StrokeThickness="0"/>
<Path
Data="M 25,20 25,80 45,80 45,20Z"
Stroke="Black"
Fill="Black"
StrokeThickness="0"/>
<Path
Data="M 50,40 50,80 70,80 70,40Z"
Stroke="Black"
Fill="Black"
StrokeThickness="0"/>
我尝试使用键(如x:Name…)在c#behind代码中引用我的Xaml元素,但它不起作用
<Grid>
<Button Click="Button_Click">
<Button.Content>
<Grid x:Name="GraphGrid" Width="Auto" Height="Auto" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<Path x:Name="Path"
Data="M 0,0 0,80 20,80 20,0Z"
Stroke="Black"
Fill="Black"
StrokeThickness="0"/>
<Path x:Name="path1"
Data="M 25,20 25,80 45,80 45,20Z"
Stroke="Black"
Fill="Black"
StrokeThickness="0"/>
<Path x:Name="Path2"
Data="M 50,40 50,80 70,80 70,40Z"
Stroke="Black"
Fill="Black"
StrokeThickness="0"/>
</Grid>
</Button.Content>
</Button>
</Grid>
我希望这会有所帮助。以下是一种在XAML中实现此功能而不需要任何代码隐藏的方法:
<Button>
<Button.Content>
<Grid x:Name="GraphGrid" Width="Auto" Height="Auto" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<Path x:Name="Path"
Data="M 0,0 0,80 20,80 20,0Z"
Stroke="Black"
Fill="Black"
StrokeThickness="0"/>
<Path x:Name="Path2"
Data="M 25,20 25,80 45,80 45,20Z"
Stroke="Black"
Fill="Black"
StrokeThickness="0"/>
<Path x:Name="Path3"
Data="M 50,40 50,80 70,80 70,40Z"
Stroke="Black"
Fill="Black"
StrokeThickness="0"/>
</Grid>
</Button.Content>
<Button.Triggers>
<EventTrigger RoutedEvent="PreviewMouseDown">
<BeginStoryboard>
<Storyboard >
<ColorAnimation Storyboard.TargetName="Path" Storyboard.TargetProperty="Fill.Color" From="Black" To="Red"></ColorAnimation>
<ColorAnimation Storyboard.TargetName="Path2" Storyboard.TargetProperty="Fill.Color" From="Black" To="Yellow"></ColorAnimation>
<ColorAnimation Storyboard.TargetName="Path3" Storyboard.TargetProperty="Fill.Color" From="Black" To="Blue"></ColorAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
@ViralShah Spam,链接将我重定向到:
<Button>
<Button.Content>
<Grid x:Name="GraphGrid" Width="Auto" Height="Auto" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<Path x:Name="Path"
Data="M 0,0 0,80 20,80 20,0Z"
Stroke="Black"
Fill="Black"
StrokeThickness="0"/>
<Path x:Name="Path2"
Data="M 25,20 25,80 45,80 45,20Z"
Stroke="Black"
Fill="Black"
StrokeThickness="0"/>
<Path x:Name="Path3"
Data="M 50,40 50,80 70,80 70,40Z"
Stroke="Black"
Fill="Black"
StrokeThickness="0"/>
</Grid>
</Button.Content>
<Button.Triggers>
<EventTrigger RoutedEvent="PreviewMouseDown">
<BeginStoryboard>
<Storyboard >
<ColorAnimation Storyboard.TargetName="Path" Storyboard.TargetProperty="Fill.Color" From="Black" To="Red"></ColorAnimation>
<ColorAnimation Storyboard.TargetName="Path2" Storyboard.TargetProperty="Fill.Color" From="Black" To="Yellow"></ColorAnimation>
<ColorAnimation Storyboard.TargetName="Path3" Storyboard.TargetProperty="Fill.Color" From="Black" To="Blue"></ColorAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>