首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVVM VisualBrush绑定

MVVM VisualBrush绑定
EN

Stack Overflow用户
提问于 2017-04-04 15:12:37
回答 1查看 421关注 0票数 0

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

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

查看:

代码语言:javascript
复制
<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>

ViewModel:

代码语言:javascript
复制
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 }
            }
        };
     ......

型号:

代码语言:javascript
复制
[PropertyChanged.ImplementPropertyChanged]
    public class WindowCommandModel {
        public string Header { get; set; }
        public ICommand Command { get; set; } = new RoutedCommand();
        public VisualBrush Icon { get; set; }
    }

我的结果是:

结果

为什么我的图标没有显示?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-04 15:47:30

代码语言:javascript
复制
<VisualBrush Stretch="Fill" Visual="{Binding Icon}" />

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

您正在绑定到一个VisualBrush

代码语言:javascript
复制
Icon = new VisualBrush() { Visual = (Visual)myResourceDictionary["users"] as Canvas}

试一试:

代码语言:javascript
复制
Icon = myResourceDictionary["users"] as Canvas
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43211137

复制
相关文章

相似问题

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