1

私の質問は wp7 についてです。ユーザーがクリックした後、コードの背後にある C# でボタンのコンテンツを変更しようとしています。特に、グリッド ("GraphGrid") 内にある 3 つの Path 要素の Fill プロパティを変更したいと考えています。このグリッドは、ボタン自体のコンテンツです。ボタンに関する 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 に答える