首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >实现可重用按钮

实现可重用按钮
EN

Stack Overflow用户
提问于 2014-12-19 13:51:28
回答 2查看 74关注 0票数 0

我想知道我如何做到这一点:

在一个对话框中,我有一个带有图像的按钮:

代码语言:javascript
运行
AI代码解释
复制
    <Button>
        <StackPanel Orientation="Horizontal">
            <StaticResourceExtension ResourceKey="Save"/> // Image for this button (floppy disk)
            <TextBlock Text="Add customer"/>
        </StackPanel>
    </Button>

这应该是一个默认的‘保存按钮’在我的项目。我怎样才能使它可重用?现在,我必须在另一个上下文中复制这段代码,比如添加文章:

代码语言:javascript
运行
AI代码解释
复制
    <Button>
        <StackPanel Orientation="Horizontal">
            <StaticResourceExtension ResourceKey="Save"/>
            <TextBlock Text="Add article"/>
        </StackPanel>
    </Button>

我想要一个地方,我可以改变它,并影响每一个‘保存’按钮在我的整个项目。我尝试过ControlTemplate,但是如果我使用它,系统默认的按钮设计就会消失。但我想要系统的默认设计。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-19 14:05:53

您可以使用样式来实现这一点。

代码语言:javascript
运行
AI代码解释
复制
<Window.Resources>
    <Style TargetType="{x:Type Button}" x:Key="saveButton">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Width="24" Height="24" Source="{StaticResource Save}"/>
                        <TextBlock Text="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"/>
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

然后用你的按钮:

代码语言:javascript
运行
AI代码解释
复制
<Grid>
    <Button Content="Save" Style="{StaticResource saveButton}"/>
    <Button Content="Add article" Style="{StaticResource saveButton}"/>
</Grid>

将资源字典中的Image替换为BitmapImage

代码语言:javascript
运行
AI代码解释
复制
<BitmapImage UriSource="Images/MenuIcons/File.png" x:Key="File" />
票数 2
EN

Stack Overflow用户

发布于 2014-12-19 14:33:33

IconDictionary:

代码语言:javascript
运行
AI代码解释
复制
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style TargetType="Image">
    <Setter Property="Height" Value="24" />
    <Setter Property="Width" Value="24" />
</Style>

<!--MENU_ICONS:-->
<Image Source="../Images/MenuIcons/KraftSolution.ico" x:Key="KraftSolution" />
<Image Source="../Images/MenuIcons/File.png" x:Key="File" />
<Image Source="../Images/MenuIcons/Close.png" x:Key="Close" />
<Image Source="../Images/MenuIcons/Customers.png" x:Key="Customers"  />
<Image Source="../Images/MenuIcons/Vehicles.png" x:Key="Vehicles"  />
<Image Source="../Images/MenuIcons/Components.png" x:Key="Components"  />
<Image Source="../Images/MenuIcons/Connection.png" x:Key="Connection"  />
<Image Source="../Images/MenuIcons/Info.png" x:Key="Info" />

<!--MENU_OPERATIONS:-->

<!--CUSTOMER:-->
<Image Source="../Images/MenuOperations/Customer/Customer_Add.png" x:Key="CustomerAdd" />
<Image Source="../Images/MenuOperations/Customer/Customer_Edit.png" x:Key="CustomerEdit" />
<Image Source="../Images/MenuOperations/Customer/Customer_Delete.png" x:Key="CustomerDelete" />

<!--VEHICLE:-->
<Image Source="../Images/MenuOperations/Vehicle/Vehicle_Add.png" x:Key="VehicleAdd" />
<Image Source="../Images/MenuOperations/Vehicle/Vehicle_Edit.png" x:Key="VehicleEdit" />
<Image Source="../Images/MenuOperations/Vehicle/Vehicle_Delete.png" x:Key="VehicleDelete" />

<!--COMPONENT:-->
<Image Source="../Images/MenuOperations/Component/Component_Add.png" x:Key="ComponentAdd" />
<Image Source="../Images/MenuOperations/Component/Component_Edit.png" x:Key="ComponentEdit" />
<Image Source="../Images/MenuOperations/Component/Component_Delete.png" x:Key="ComponentDelete" />

<!--TAB CONTROL:-->
<Image Source="../Images/MenuIcons/Notes.png" x:Key="Notes" />
<Image Source="../Images/MenuIcons/Contact.png" x:Key="Contact" />

<!--SEARCH_BUTTON:-->
<Image Source="../Images/MenuIcons/Search.png" x:Key="Search" />

<Image Source="../Images/MenuOperations/Save.png" x:Key="Save" Height="24" Width="24"/>
<Image Source="../Images/MenuOperations/Abort.png" x:Key="Abort" />

为了解决我的问题,我在保存图像中添加了Height="24“Width=24。但我不想那样。我想要一个位置,我可以控制图像大小。

您的风格在另一个资源字典中,我在窗口中包含了两个字典,在其中我使用了按钮和图像。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27574389

复制
相关文章

相似问题

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