2012-11-19 45 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> 
相關問題