我的问题是关于wp7。我试图在用户点击后改变c#后面代码中的按钮内容。特别是我想更改我的三个Path元素的Fill属性,它们位于网格内(“GraphGrid”)。此网格是Button本身的内容。 这是关于Button的XAML代码:
<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#中的我的Xaml元素,但它不起作用。
答案 0 :(得分:2)
<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>
private void Button_Click(object sender, RoutedEventArgs e)
{
path1.Fill = new SolidColorBrush(Colors.AliceBlue);
Path2.Fill = new SolidColorBrush(Colors.Pink);
Path.Fill = new SolidColorBrush(Colors.Red);
}
我希望这会有所帮助。
答案 1 :(得分:1)
以下是在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>