【问题标题】:WPF: modifying property of elements in common control templateWPF:修改通用控件模板中元素的属性
【发布时间】:2013-08-17 07:30:48
【问题描述】:

我为Resources 中的按钮创建了一个ControlTemplate,如下所示:

<ControlTemplate x:Key="buttonCtrlTemp" TargetType="{x:Type Button}">
                    <DockPanel x:Name="dock">
                        <Image x:Name="btnImg" Height="16" Width="16" DockPanel.Dock="Left"/>
                        <TextBlock VerticalAlignment="Center" Text="{TemplateBinding Button.Content}"/>
                    </DockPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Button.IsMouseOver" Value="True">
                            <Setter TargetName="dock" Property="Background" Value="{StaticResource AppBlue}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>

我从按钮中将其引用为

&lt;Button Content="Login" Template="{StaticResource buttonCtrlTemp}"/&gt;

但是,我想为不同的按钮设置不同的图像,因此需要一些方法在按钮的控件模板中设置 Image 元素的来源。我该怎么做?

【问题讨论】:

    标签: .net wpf controltemplate


    【解决方案1】:

    在这种情况下,您可以使用Tag。示例:

    Template:

    <Image x:Name="btnImg" Source="{TemplateBinding Tag}" Height="16" Width="16" DockPanel.Dock="Left" />
    

    使用:

    <!-- In Resources -->
    <BitmapImage x:Key="MyFind" UriSource="/BlackFind.jpg" />
    
    <Button Name="FindTestButton" Tag="{StaticResource MyFind}" Template="{StaticResource buttonCtrlTemp}" ... />
    

    在模板中最好使用ContentPresenter 而不是TextBlock。因为这个控件负责显示控件的内容,这是他唯一的目标。因此,首先,它小于“重量”(几乎所有控件都有您的ContentPresenter),其次,内容可能是通用类型。完整示例:

    <Window.Resources>
        <BitmapImage x:Key="MyFind" UriSource="/BlackFind.jpg" />
        <BitmapImage x:Key="MyAttach" UriSource="/attachment.png" />
    
        <ControlTemplate x:Key="buttonCtrlTemp" TargetType="{x:Type Button}">
            <DockPanel x:Name="dock" Background="{TemplateBinding Background}">
                <Image x:Name="btnImg" Source="{TemplateBinding Tag}" Height="16" Width="16" DockPanel.Dock="Left" />
                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True" />
            </DockPanel>
    
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="dock" Property="Background" Value="Gray" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Window.Resources>
    
    <Grid>
        <Button Name="FindTestButton" Width="100" Tag="{StaticResource MyFind}" Background="Gainsboro" Content="FindButton" Height="30" Template="{StaticResource buttonCtrlTemp}" />
        <Button Name="AttachTestButton" Width="100" Tag="{StaticResource MyAttach}" Background="Gainsboro" Content="AttachButton" Height="30" Template="{StaticResource buttonCtrlTemp}" Margin="0,80,0,0" />
    </Grid>
    

    Output

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多