【问题标题】:Button and ToggleButton in wpf Toolbar using MVVM使用 MVVM 的 wpf 工具栏中的按钮和切换按钮
【发布时间】:2012-08-28 09:50:41
【问题描述】:

我的应用程序内的 ToolbarTray 中有一个 wpf 工具栏,它必须承载按钮和切换按钮。 有人可以建议我如何在 MVVM 中实现这种行为吗? 下面的代码就是我现在所拥有的:

 <ToolBarTray Margin="5,30,5,30" MinWidth="35" HorizontalAlignment="Center" Orientation="Vertical" Background="Transparent">
            <ToolBar x:Name="ToolBarControl" HorizontalAlignment="Stretch" ItemsSource="{Binding ToolBarItems}" >
                <ToolBar.ItemTemplate>
                    <DataTemplate>
                        <Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Stretch" Margin="0,0,0,15"
                        Template="{Binding ToolBarIcon}" 
                        cal:Message.Attach="[Event Click] = [Action Select()]"
                        IsEnabled="True"
                        ToolTip="{Binding Text}"/>
                    </DataTemplate>
                </ToolBar.ItemTemplate>
            </ToolBar>
        </ToolBarTray>

按钮可以是普通按钮或切换按钮。 提前致谢。

【问题讨论】:

    标签: wpf button datatemplate toolbar togglebutton


    【解决方案1】:

    在 MVVM 模式中,您的模型类将包含您要绑定并在视图中显示的属性(所有数据对象)。所以 ToolBarItems 集合将成为模型的一部分。

    您的视图基本上将包含您编写的上述代码。并且在代码隐藏文件中,会有一个模型类类型的对象作为属性。

    您的视图模型可以初始化模型和视图对象并将模型绑定到视图的数据上下文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      相关资源
      最近更新 更多