1

我的问题是关于 wp7。在用户单击它后,我正在尝试在 c# 代码后面更改按钮的内容。特别是我想更改我的三个 Path 元素的 Fill 属性,它们位于网格(“GraphGrid”)内。这个网格是按钮本身的内容。这是关于 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 元素,但它不起作用。

4

2 回答 2

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);
    }

我希望这将有所帮助。

于 2012-11-19T11:13:34.437 回答
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>
于 2012-11-19T11:17:46.050 回答