繁体   English   中英

如何在Mahapps.Metro标题栏中将XAML资源用作图标?

[英]How To Use a XAML Resource As An Icon In Mahapps.Metro Title Bar?

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

这是我的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并使用常规绑定来实现:

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];
    }
}

视图模型:

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));
        }
    }
}

在此处输入图片说明 在此处输入图片说明

尽管已经接受了该问题的答案,但这是我的解决方案,不需要MetroWindow子类中的代码(尽管它确实需要在视图模型中引用System.Windows和使用Application Dispatcher)。

using System;
using Caliburn.Micro;
using System.Windows.Media;
using System.Windows;

namespace ChangingMahAppsIcon
{
    class CHeartbeatViewModel : PropertyChangedBase
    {
        public Visual IconResource { get; set; }

        private System.Timers.Timer m_oHeartbeatTimer;
        private ResourceDictionary m_oIconResource;
        private bool m_bHeartbeatIsOn = false;

        public CHeartbeatViewModel()
        {
            m_oIconResource = new ResourceDictionary() { Source = new Uri(@"Resources\Icons.xaml", UriKind.Relative) };

            m_oHeartbeatTimer = new System.Timers.Timer(1000);
            m_oHeartbeatTimer.Elapsed += UpdateHeartbeat;

            m_oHeartbeatTimer.Start();

        }

        private void UpdateHeartbeat(object sender, System.Timers.ElapsedEventArgs e)
        {
            string strIconName;
            m_bHeartbeatIsOn = !m_bHeartbeatIsOn;
            if (m_bHeartbeatIsOn)
            {
                strIconName = "appbar_heart";
            }
            else
            {
                strIconName = "appbar_heart_outline";
            }

            Application.Current.Dispatcher.Invoke((System.Action)delegate {

                IconResource = (Visual)m_oIconResource[strIconName];
                NotifyOfPropertyChange(() => IconResource);
            });

        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM