【问题标题】:How can I make a Button with multiple content values?如何制作具有多个内容值的 Button?
【发布时间】:2010-09-26 17:06:53
【问题描述】:

我的目标是制作一个具有两个 Content 值的 Button。

将 Scrabble 拼字块想象成一个按钮:它的中心有一个大字母,右下角有一个小数字。这就是我想要的效果。

我制作了一个按钮,其中包含两个 ContentPresenter 对象,并且我为每个 ContentPresenter 赋予了不同的样式。但是,我还没有找到一种方法给每个演示者一个单独的值(即,如果我将按钮的内容设置为“X”,那么两个 ContentPresenter 都显示“X”,尽管样式不同)。

我怎样才能实现我的目标?我猜我的方法是完全错误的......

【问题讨论】:

    标签: wpf xaml contentpresenter


    【解决方案1】:

    呸……我想我现在知道该怎么做了。我应该自己控制而不是修改按钮。如果我一直在 WinForms 中工作,这对我来说是显而易见的,但由于某种原因,所有这些 Xaml 都让我变得愚蠢。

    【讨论】:

      【解决方案2】:

      查看 http://msdn.microsoft.com/en-us/library/ms753296.aspx 上的 Expander 示例 ControlTemplate

      Expander 是 HeaderedContentControl 的子类,它有两个“内容”:Header 和 Content

      控件模板有两个ContentPresenter元素,未绑定默认内容属性的ContentPresenter定义为:

      如果您需要使用 Button 并且不想为第二个内容添加另一个属性,您可以使用附加属性并将第二个 ContentPresnter Content 属性数据绑定到它。

      【讨论】:

        【解决方案3】:

        我对创建具有多个“内容槽”here 的 UserControl 感到满意 - 这比从 HeaderedControl 派生更好,因为您不受槽数量的限制。

        示例用法:

        <Window x:Class="TkMVVMContainersSample.Services.TaskEditDialog.ItemEditView"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:Common="clr-namespace:TkMVVMContainersSample.Views.Common"
            Title="ItemEditView"
            >
            <Common:DialogControl>
                <Common:DialogControl.Heading>
                    <!-- Heading string goes here -->
                </Common:DialogControl.Heading>
                <Common:DialogControl.Control>
                    <!-- Concrete dialog's content goes here -->
                </Common:DialogControl.Control>
                <Common:DialogControl.Buttons>
                    <!-- Concrete dialog's buttons go here -->
                </Common:DialogControl.Buttons>
            </Common:DialogControl>
        
        </Window>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-03-30
          • 2021-12-18
          • 1970-01-01
          • 2021-01-20
          • 2016-02-14
          • 2012-04-24
          • 1970-01-01
          相关资源
          最近更新 更多