【问题标题】:WPF problem modify style of images in multiple Button TemplatesWPF问题修改多个按钮模板中的图像样式
【发布时间】:2010-12-28 20:47:25
【问题描述】:

我正在尝试为摄像机创建一个控制面板,该面板具有向上、向下、向左和向右等按钮。向上、向下、向左和向右的每个摄像机功能由三个图像表示(参见下面的代码) 用于左侧、中间和右侧。控制面板是圆形的,所以角落图像有点重叠(如果没有视觉来解释这一点很复杂)。例如,当我单击 up 时,我必须隐藏最初的三个图像(左侧、中间和右侧)并显示另外三个表示按钮被按下的左侧、中间和右侧的图像。我通过在按钮模板中有一个网格来实现这一点。我遇到的问题是,对于控件的角图像,实际上有四个图像代表了这一点。例如,对于左上角,四个图像将表示 1。顶部未单击。 2. 顶部单击和 3. 左键未单击和 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>

【问题讨论】:

    标签: wpf


    【解决方案1】:

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

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

    在后面的代码中。

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 2011-02-10
      • 2014-11-05
      • 1970-01-01
      相关资源
      最近更新 更多