【问题标题】:MVVM VisualBrush BindingMVVM VisualBrush 绑定
【发布时间】:2017-04-04 23:47:30
【问题描述】:

不知道这是否是“最好”的方法。 我正在学习 MVVM 的概念,对此感到抱歉。

我有 4 个按钮,我想在一个矩形内显​​示一个 VisualBrush。 这是我的代码:

查看:

<Controls:MetroWindow.RightWindowCommands>
    <Controls:WindowCommands Name="WindowCommand" ItemsSource="{Binding Model.WindowCommandItems}">
        <Controls:WindowCommands.ItemTemplate>
            <DataTemplate DataType="{x:Type ModelType:MainModel}">
                <Button Command="{Binding Command}">
                    <StackPanel Orientation="Horizontal">
                        <Rectangle Width="20"
                           Height="20">
                            <Rectangle.OpacityMask>
                                <VisualBrush Stretch="Fill" Visual="{Binding Icon}" />
                            </Rectangle.OpacityMask>
                        </Rectangle>
                        <TextBlock Margin="4 0 0 0"
                           VerticalAlignment="Center"
                           Text="{Binding Header}" />
                    </StackPanel>
                </Button>
            </DataTemplate>
        </Controls:WindowCommands.ItemTemplate>
    </Controls:WindowCommands>
</Controls:MetroWindow.RightWindowCommands>

视图模型:

private void CreateWindowCommands() {
        var myResourceDictionary = new ResourceDictionary();
        myResourceDictionary.Source =
            new Uri("/BlackBoxBot;component/Resources/Icons.xaml",
                    UriKind.RelativeOrAbsolute);

        Model.WindowCommandItems = new ObservableCollection<Models.MainModel.WindowCommandModel> {
            new Models.MainModel.WindowCommandModel {
                Header = "Viewer",
                Icon = new VisualBrush() { Visual = (Visual)myResourceDictionary["users"] as Canvas}
            },
            new Models.MainModel.WindowCommandModel {
                Header = "Home",
                Icon = new VisualBrush() { Visual = (Visual)myResourceDictionary["appbar_home"] as Canvas }
            },
            new Models.MainModel.WindowCommandModel {
                Header = "Dashboard",
                Icon = new VisualBrush() { Visual = (Visual)myResourceDictionary["theater"] as Canvas }
            },
            new Models.MainModel.WindowCommandModel {
                Header = "Einstellungen",
                Icon = new VisualBrush() { Visual = (Visual)myResourceDictionary["settings"] as Canvas }
            }
        };
     ......

型号:

[PropertyChanged.ImplementPropertyChanged]
    public class WindowCommandModel {
        public string Header { get; set; }
        public ICommand Command { get; set; } = new RoutedCommand();
        public VisualBrush Icon { get; set; }
    }

我的结果:

Result

为什么我的图标不显示?

【问题讨论】:

    标签: c# mvvm mahapps.metro


    【解决方案1】:
    <VisualBrush Stretch="Fill" Visual="{Binding Icon}" />
    

    VisualBrush.Visual 期望对象的类型为Visual

    您正在绑定到VisualBrush

    Icon = new VisualBrush() { Visual = (Visual)myResourceDictionary["users"] as Canvas}
    

    试试这个:

    Icon = myResourceDictionary["users"] as Canvas
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 2015-08-06
      • 2011-02-22
      • 2013-06-14
      • 2021-03-01
      • 2013-01-07
      相关资源
      最近更新 更多