首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >资源医学中的Wpf数据库

资源医学中的Wpf数据库
EN

Stack Overflow用户
提问于 2016-05-19 19:20:48
回答 2查看 1.3K关注 0票数 2

我最近开始使用C# WPF,因为它有更多的选项来装饰控件。现在,我想尝试制作一个按钮,按钮上有一个图标,旁边有一个文本,经过搜索,我找到了a post that seemed helpfull。我认为我得到我想要的东西的方法是正确的,但是我不知道我如何能做到这一点,所以我可以为每个按钮分别指定图标。

下面是我当前用于按钮的代码:

代码语言:javascript
复制
<!-- Resource Dictionary-->
<Style TargetType="{x:Type Button}" x:Key="Test">
    <Setter Property="Background" Value="#373737" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontSize" Value="15" />
    <Setter Property="SnapsToDevicePixels" Value="True" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border CornerRadius="4" Background="{TemplateBinding Background}">
                    <Grid>
                        <Canvas x:Name="ButtonImage" Width="40" Height="40" HorizontalAlignment="Left" Margin="10,0,0,0">
                            <Canvas.Background>
                                <ImageBrush ImageSource="F:\Deze Pc\Afbeeldingen\Icons\Steel Series\Documents.ico"/>
                            </Canvas.Background>
                        </Canvas>
                        <Path x:Name="PathIcon" Width="15" Height="25" Stretch="Fill" Fill="#4C87B3" HorizontalAlignment="Left" Margin="20,0,0,0" />
                        <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="60,0,0,0" />
                    </Grid>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#E59400" />
                        <Setter Property="Foreground" Value="White" />
                        <Setter TargetName="PathIcon" Property="Fill" Value="Black" />
                    </Trigger>

                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background" Value="OrangeRed" />
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!--Window Xaml -->
<Button Width="200" Height="50" VerticalAlignment="Top"  Grid.Row="2" Style="{StaticResource Test}" >
        <Button.Content>
            <StackPanel>
                <TextBlock Text="Button text here" FontSize="20" />
            </StackPanel>
        </Button.Content>
    </Button>

但现在是我的问题,我如何使它,我可以设置在窗口xaml一侧,而不是在资源字典中,这样我可以给不同的图标不同的按钮,而不需要为他们每个风格。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-20 03:12:23

假设您还没有在模板中使用DataContext,我认为最简单的方法就是绑定到它。也就是说,在你的模板中:

代码语言:javascript
复制
<Canvas x:Name="ButtonImage" Width="40" Height="40" HorizontalAlignment="Left" Margin="10,0,0,0">
    <Canvas.Background>
        <ImageBrush ImageSource="{Binding}"/>
    </Canvas.Background>
</Canvas>

然后在使用样式的地方:

代码语言:javascript
复制
<Button Width="200" Height="50" VerticalAlignment="Top"
        Grid.Row="2" Style="{StaticResource Test}"
        DataContext="{StaticResource imageSource1}">
    <Button.Content>
        <StackPanel>
            <TextBlock Text="Button text here" FontSize="20" />
        </StackPanel>
    </Button.Content>
</Button>

当然,您已经将适当的图像源声明为资源,例如:

代码语言:javascript
复制
<BitmapImage x:Key="imageSource1"
             UriSource="F:\Deze Pc\Afbeeldingen\Icons\Steel Series\Documents.ico"/>

备选办法包括:

  1. 为了同样的目的使用Tag属性。在本例中,情况要复杂一些:您需要将Tag属性向下传递到Border元素,如<Border x:Name="root" Tag="{TemplateBinding Tag}" ...>,然后在模板中将其引用为例如<ImageBrush ImageSource="{Binding Tag, ElementName=root}"/>
  2. 创建特定的数据结构以保存所需的属性值。在本例中,您仍然需要使用DataContext并通过{Binding PropertyName}语法访问属性,因此这并不比我上面的主要建议更好。如果您发现需要将多个值从客户端站点传递到模板,则可以使用它。
票数 1
EN

Stack Overflow用户

发布于 2016-05-19 20:47:35

我相信你需要像下面这样使用DynamicResource

代码语言:javascript
复制
<Image Source="{DynamicResource ResourceKey=Img}"/>

Referance

或者使用RelativeSource

代码语言:javascript
复制
<Image Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37332305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档