WPF问题修改多个按钮模板中的图像样式

时间:2010-12-28 20:47:25

标签: wpf

我正在尝试为摄像机创建一个控制面板,该面板具有向上,向下,向左和向右等按钮。每个摄像机功能上,下,左和右由三个图像表示(参见下面的代码)左侧,中侧和右侧。控制面板是圆形的,因此角落图像有点重叠(很复杂,无需视觉就能解释这一点)。例如,当我点击向上时,我必须隐藏最初的三个图像(左侧,中间和右侧),并显示左侧,中间和右侧的另外三个图像,表示按下按钮。我通过在按钮模板中放置一个网格来实现这一点。我的问题是,对于控件的角落图像,实际上有四个图像代表这一点。例如,对于左上角,四个图像将代表1.顶部未单击。 2. Top Clicked和3. Left未点击和4.左键单击​​。我的问题是,如果我需要在单击顶部控件时使顶部按钮中包含的图像具有优先权,或者在单击左侧按钮时使左侧按钮中的图像具有优先权。因此,当我点击顶部按钮时,我想修改左按钮的图像可见属性,反之亦然。这真的很难解释所以我道歉如果没有意义但我可以根据要求通过电子邮件发送源代码,如果有人对我的困境感兴趣的话。

<Grid>

    <Canvas>



            <!--<StackPanel>-->
        <Button Name="TopSide" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Height="34" Width="102" 
    Canvas.Left="97" Canvas.Top="60" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   >

        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid Width="100">
                    <Canvas>
                        <Image Name="TopRightNormal" Source="Resources/topright_off.jpg" Height="34" Width="34" Canvas.Left="66"></Image>
                        <Image Name="TopRightDown" Source="Resources/topright_down.jpg" Height="34" Width="34" Canvas.Left="66"  Visibility="Hidden" ></Image>

                        <Image Name="TopNormal" Source="Resources/topcenter_off.jpg" Height="34" Width="34" Canvas.Left="34" />
                        <Image Name="TopPressed" Source="Resources/topcenter_down.jpg" Height="34" Width="34" Canvas.Left="34" Visibility="Hidden" />
                        <Image Name="TopDisabled" Source="Resources/topcenter_off.jpg" Height="34" Width="34" Canvas.Left="34" Visibility="Hidden" />

                        <Image Name="TopLeftNormal" Source="Resources/topleft_off.jpg" Height="34" Width="34" Canvas.Left="2" ></Image>
                        <Image Name="TopLeftDown" Opacity="0" Source="Resources/topleft_down.jpg" Height="34" Width="34" Canvas.Left="2" Visibility="Hidden" ></Image>

                    </Canvas>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="TopNormal" Property="Visibility" Value="Hidden" />
                        <Setter TargetName="TopPressed" Property="Visibility" Value="Visible" />

                        <Setter TargetName="TopRightNormal" Property="Visibility" Value="Hidden" />
                        <Setter TargetName="TopRightDown" Property="Visibility" Value="Visible" />

                        <Setter TargetName="TopLeftNormal" Property="Visibility" Value="Hidden" />
                        <Setter TargetName="TopLeftDown" Property="Visibility" Value="Visible"  />
                        <Setter TargetName="TopLeftDown" Property="Opacity"  Value="100"  />

                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="TopNormal" Property="Visibility" Value="Hidden" />
                        <Setter TargetName="TopDisabled" Property="Visibility" Value="Visible" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Button.Template>

    </Button>

    <!--</StackPanel>-->

    <!--<StackPanel>-->
    <Button Name="LeftSide" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            Canvas.Left="100" Canvas.Top="60" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MouseDown="Button_MouseDown_1">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid  Width="34" Height="100">
                    <Canvas>

                        <Image Name="TopLeftNormal" Source="Resources/topleft_off.jpg" Height="34" Width="34" Canvas.Left="0"></Image>
                        <Image  Name="TopLeftDown" Opacity="0" Source="Resources/topleft_leftdown.jpg" Height="34" Width="34" Canvas.Left="0"  Visibility="Hidden" ></Image>


                        <Image Name="Normal" Source="Resources/leftcenter_off.jpg" Height="34" Width="34" Canvas.Top="32" Canvas.Left="0"/>
                        <Image Name="Pressed" Source="Resources/leftcenter_down.jpg" Visibility="Hidden"  Canvas.Top="32"  Height="34" Width="34" />
                        <Image Name="Disabled" Source="Resources/leftcenter_off.jpg" Visibility="Hidden" Height="34" Width="34" Canvas.Top="32"  />
                    </Canvas>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Normal" Property="Visibility" Value="Hidden" />
                        <Setter TargetName="Pressed" Property="Visibility" Value="Visible" />

                        <Setter TargetName="TopLeftNormal" Property="Visibility" Value="Hidden" />
                        <Setter TargetName="TopLeftNormal" Property="Opacity"  Value="0"  />

                        <Setter TargetName="TopLeftDown" Property="Visibility" Value="Visible"  />
                        <Setter TargetName="TopLeftDown" Property="Opacity"  Value="100"  />

                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Normal" Property="Visibility" Value="Hidden" />
                        <Setter TargetName="Disabled" Property="Visibility" Value="Visible" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Button.Template>
    </Button>
    <!--</StackPanel>-->


    <!--<StackPanel>-->
    <Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Height="34" Width="34" 
    Canvas.Left="165" Canvas.Top="92" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MouseDown="Button_MouseDown_2" >
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Image Name="Normal" Source="Resources/rightcenter_off.jpg" />
                    <Image Name="Pressed" Source="Resources/rightcenter_down.jpg" Visibility="Hidden" />
                    <Image Name="Disabled" Source="Resources/rightcenter_off.jpg" Visibility="Hidden" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="Normal" Property="Visibility" Value="Hidden" />
                        <Setter TargetName="Pressed" Property="Visibility" Value="Visible" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Normal" Property="Visibility" Value="Hidden" />
                        <Setter TargetName="Disabled" Property="Visibility" Value="Visible" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Button.Template>
    </Button>
    <!--</StackPanel>-->

    <!--<StackPanel>-->
    <Button xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Height="34" Width="34" 
    Canvas.Left="133" Canvas.Top="124" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  >
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Image Name="BottomNormal" Source="Resources/bottomcenter_off.jpg" />
                    <Image Name="BottomPressed" Source="Resources/bottomcenter_down.jpg" Visibility="Hidden" />
                    <Image Name="BottomDisabled" Source="Resources/bottomcenter_off.jpg" Visibility="Hidden" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="BottomNormal" Property="Visibility" Value="Hidden" />
                        <Setter TargetName="BottomPressed" Property="Visibility" Value="Visible" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="BottomNormal" Property="Visibility" Value="Hidden" />
                        <Setter TargetName="BottomDisabled" Property="Visibility" Value="Visible" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Button.Template>
    </Button>
    <!--</StackPanel>-->


    <Image Source="Resources/bottomright_off.jpg" Height="34" Width="34" Canvas.Left="165" Canvas.Top="124"></Image>
    <Image Source="Resources/bottomleft_off.jpg" Height="34" Width="34" Canvas.Left="100" Canvas.Top="124"></Image>
    <!--<ToggleButton Style="{StaticResource MyToggleButtonStyle}" Height="34" Width="34"  Margin="150,100"/>-->


    </Canvas>
</Grid>

1 个答案:

答案 0 :(得分:0)

好的,我找到了一种方法来访问按钮的Button.Template中的图像,该按钮的子控件我想要隐藏。我用了

Image imgTopLeftNormal = LeftSide.Template.FindName(“TopLeftNormal”,LeftSide)as Image;

在后面的代码中。