c# - 如何使用 XAML 资源作为 Mahapps.Metro 标题栏中的图标?

标签 c# wpf xaml mahapps.metro xaml-resources

我有一个小型应用程序,使用 Mahapps Metro 框架。我想更改标题栏中的图标,例如连接/断开连接图标。我如何访问内容或动态绑定(bind)它?

这是我的 XAML:

<Controls:MetroWindow x:Class="NFCAgent.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                  Title="NFC-Agent"
                  Height="55"
                  Width="250"
                  ResizeMode="NoResize"
                  GlowBrush="{DynamicResource AccentColorBrush}"
                  WindowStartupLocation="Manual"
                  Topmost="True"
                  ShowIconOnTitleBar="True">
<Controls:MetroWindow.LeftWindowCommands>
    <Controls:WindowCommands>
        <Button IsEnabled="False" >
            <StackPanel Orientation="Horizontal">
                <Rectangle x:Name="headerLogo" Width="20" Height="20"
               Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}">
                    <Rectangle.OpacityMask>
                        <VisualBrush Stretch="Fill"
                     Visual="{DynamicResource appbar_cupcake}" />
                    </Rectangle.OpacityMask>
                </Rectangle>
            </StackPanel>
        </Button>
    </Controls:WindowCommands>
</Controls:MetroWindow.LeftWindowCommands>
<Grid>
    <StackPanel Orientation="Horizontal">
        <Rectangle Width="25" Height="25" Margin="4 0">
            <Rectangle.Fill>
                <VisualBrush Visual="{DynamicRessource appbar_add}" />
            </Rectangle.Fill>
        </Rectangle>
        <TextBlock x:Name="txtStatus" Text="Statusinfo" VerticalAlignment="Center"></TextBlock>
    </StackPanel>
</Grid>

补充:我没有 ImageFile(没有 *.ico),我使用 Mahapps.Resources(xaml 文件)中包含的资源。 Afaik 矩形的内容必须以某种方式替换 - 但如何替换?

最佳答案

您可以通过将图标资源保留在 ViewModel 中并使用常规绑定(bind)来实现这一点:

XAML:

<Controls:MetroWindow x:Class="WpfApplication4.Window4"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
    xmlns:local="clr-namespace:WpfApplication4"
    mc:Ignorable="d"
    BorderThickness="5"        
    BorderBrush="{DynamicResource AccentColorBrush}"
    Title="{Binding MyIcon}" Height="300" Width="300" Loaded="MetroWindow_Loaded">
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro.Resources;component/Icons.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Olive.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

<Controls:MetroWindow.LeftWindowCommands>
    <Controls:WindowCommands>
        <Rectangle Height="20" Width="20" Fill="{Binding RelativeSource={RelativeSource AncestorType=Controls:WindowCommands}, Path=Foreground}">
            <Rectangle.OpacityMask>
                <VisualBrush Stretch="Fill" Visual="{Binding MyResource}" />
            </Rectangle.OpacityMask>
        </Rectangle>
    </Controls:WindowCommands>
</Controls:MetroWindow.LeftWindowCommands>

<Controls:MetroWindow.DataContext>
    <local:MyViewModel/>
</Controls:MetroWindow.DataContext>

<Grid>
    <Button Content="Change Icon" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Center"></Button>
</Grid>

窗口:

public partial class Window4 : MetroWindow
{
    MyViewModel viewModel;

    public Window4()
    {
        InitializeComponent();
    }

    private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
    {
        viewModel = new MyViewModel();
        DataContext = viewModel;

        viewModel.MyIcon = "appbar_box";
        viewModel.MyResource = (Visual)Resources[viewModel.MyIcon]; 
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (viewModel.MyIcon == "appbar_box")
            viewModel.MyIcon = "appbar_add";
        else viewModel.MyIcon = "appbar_box";

        viewModel.MyResource = (Visual)Resources[viewModel.MyIcon];
    }
}

View 模型:

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public MyViewModel()
    {
    }

    private string myIcon;
    public string MyIcon
    {
        get { return myIcon; }
        set
        {
            if (value != myIcon)
            {
                myIcon = value;
                NotifyPropertyChanged();
            }
        }
    }

    private Visual myResource;
    public Visual MyResource
    {
        get { return myResource; }
        set
        {
            if (value != myResource)
            {
                myResource = value;
                NotifyPropertyChanged();
            }
        }
    }

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

enter image description here enter image description here

关于c# - 如何使用 XAML 资源作为 Mahapps.Metro 标题栏中的图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34436529/

相关文章:

wpf - 触发 Button.Click 时的 XAML Binding.UpdateSourceTrigger?

xml - 在 Visual Studio 2010 中格式化 XML 的工具

c# - 检测弹出窗口何时/是否决定重新定位自身

c# - EF6 POCO 无法在 1..* 关系中转换 HashSet 类型的对象

c# - 将 IEnumerable 添加到 RouteValueDictionary - 这可能吗?

c# - 为什么 HTTP 请求永远不会返回异步等待?

wpf - 如何使用 WPF 可视化一个简单的二维世界( map 和元素)

c# - 在 XAML 中查找打开的窗口

c# - 帧之间的 UWP 导航而不会丢失汉堡菜单

c# - 遍历 WPF 的 Xceed DataGrid 中的所有单元格?