2012-11-19 73 views
1

我的问题是关于wp7。我试图在用户单击后在c#后面的代码中更改按钮的内容。特别是我想改变我的三个Path元素的填充属性,它们在网格内(“GraphGrid”)。这个网格是按钮本身的内容。 这里是关于按钮XAML代码:从c#后面的代码更改按钮的内容

<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:名称......)请参阅我的XAML元素在后面的代码C#,但它不没有工作。

+0

@ViralShah垃圾邮件的垃圾邮件的垃圾邮件,http://bit.ly/UOJqxi链接重定向我:HTTP://cloudtweak.wordpress .com /终身会员/ – SynerCoder

回答

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

我希望这将有助于。

+0

现在它的工作原理!感谢您的帮助! –

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> 
相关问题