Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用MashAPPs.Metro在XAML中设置窗口标题中的图像_C#_Wpf_Xaml_Mahapps.metro - Fatal编程技术网

C# 如何使用MashAPPs.Metro在XAML中设置窗口标题中的图像

C# 如何使用MashAPPs.Metro在XAML中设置窗口标题中的图像,c#,wpf,xaml,mahapps.metro,C#,Wpf,Xaml,Mahapps.metro,我有一个窗口,希望设置图片而不是标题文本,但它只显示图像名称空间的字符串 代码: 图片: 图标显示正确,图像保存为位图图像 如何在窗口标题中设置图像而不是普通文本?既然您使用的是MahApps控件,您需要这样做: <Controls:MetroWindow.IconTemplate> <DataTemplate> <Grid Width="{TemplateBinding Width}" Height="{T

我有一个窗口,希望设置图片而不是标题文本,但它只显示图像名称空间的字符串

代码:


图片:

图标显示正确,图像保存为位图图像


如何在窗口标题中设置图像而不是普通文本?

既然您使用的是MahApps控件,您需要这样做:

<Controls:MetroWindow.IconTemplate>
    <DataTemplate>
        <Grid Width="{TemplateBinding Width}"
              Height="{TemplateBinding Height}"
              Margin="8 8 0 8"
              Background="Transparent"
              RenderOptions.EdgeMode="Aliased"
              RenderOptions.BitmapScalingMode="HighQuality">
            <Rectangle Fill="White">
                <Rectangle.OpacityMask>
                    <VisualBrush Visual="{StaticResource appbar_resource_group}" Stretch="Uniform"/>
                </Rectangle.OpacityMask>
            </Rectangle>
        </Grid>
    </DataTemplate>
</Controls:MetroWindow.IconTemplate>


如果需要的话,您可以使用视觉画笔的图像控件。

我是通过图像的LeftWindow命令和菜单的Right Window命令实现的

代码:


图画


相反,为什么不将图像存储在应用程序根文件夹中并指定图像的路径。我使带有MVVM模式窗口图标的程序对所有窗口都是通用的,对吗?那么为什么要使用MVVM模式?只需指定图标的路径。对,但我使用MVVM,因为我从服务器上的图标池中获取图标,并且经常重复使用它使用mvvm使其更具可折叠性,如果有什么变化,我必须只更改一个资源,等等。
<Controls:MetroWindow.IconTemplate>
    <DataTemplate>
        <Grid Width="{TemplateBinding Width}"
              Height="{TemplateBinding Height}"
              Margin="8 8 0 8"
              Background="Transparent"
              RenderOptions.EdgeMode="Aliased"
              RenderOptions.BitmapScalingMode="HighQuality">
            <Rectangle Fill="White">
                <Rectangle.OpacityMask>
                    <VisualBrush Visual="{StaticResource appbar_resource_group}" Stretch="Uniform"/>
                </Rectangle.OpacityMask>
            </Rectangle>
        </Grid>
    </DataTemplate>
</Controls:MetroWindow.IconTemplate>
<Controls:MetroWindow.LeftWindowCommands>
 <Controls:WindowCommands>
   <Image Source="{Binding ImageViewModel.TitleBarImage}"/>
 </Controls:WindowCommands>
</Controls:MetroWindow.LeftWindowCommands>

<Controls:MetroWindow.RightWindowCommands>
    <Controls:WindowCommands>
        <VisualBrush Stretch="Fill"/>
        <StackPanel Name="menuHolder" Orientation="Horizontal">
            <Menu Name="MenuBar">
                <MenuItem Name="Options" Header="Options" >
                    <MenuItem Name="Settings" Header="Settings"/>
                    <MenuItem Name="Close" Header="Close"/>
                </MenuItem>
            </Menu>
        </StackPanel>
    </Controls:WindowCommands>
</Controls:MetroWindow.RightWindowCommands>