C# WPF Tips

Windowsフォームプログラミング、WPFプログラミング゙のTipsを紹介します。極力、実務に役立つように作成しました


5.Hyperlink

戻る

Hyperlink

ハイパーリンクをクリックしますと該当のURLが規定のブラウザーで開く機能です。画面は下記の通りです。

起動時

クリックすると拡大します


リンクをクリックした時

クリックすると拡大します


下記がハイパーリンクのZAMLの定義です。CommandはOpenWebsiteCommand2です。


View

<TextBlock  Padding="0,4,0,0" TextAlignment="Center" HorizontalAlignment="Left"  VerticalAlignment="Top" 
		Margin="112,97,0,0" Background="#CCFFFF" Name="txtHyperlink" Height="19" Width="166"> 
	<Hyperlink Command="{Binding OpenWebsiteCommand2}" 
		CommandParameter="http://www.aa.cyberhome.ne.jp/~bel/">
                  <!--Label-->
		c# Programing Academy
	</Hyperlink>
</TextBlock>

Ads by Google



ViewModelは下記の通りです。OpenWebsiteCommand2は、ListenerCommandを使った例で、OpenWebsiteCommandはRelayCommandを使った例です。


ViewModel

using System;

// ObservableCollection
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

//INotifyPropertyChanged
//PropertyChanged
using System.ComponentModel;

//参照設定が必要
//using System.Configuration;

using Livet;
using Livet.Commands;
using Livet.Messaging;

//CloseCommand
using Livet.Messaging.Windows;

//MessageBox
using System.Windows;

//ICommand
using System.Windows.Input;

// ListCollectionView
using System.Windows.Data;

//ArrayList
using System.Collections;

//XmlReader
using System.Xml;

//XDocument
using System.Xml.Linq;

//XPathDocument
using System.Xml.XPath;

using LivetWPFChromeHelpDesk1.Models;
using LivetWPFChromeHelpDesk1.Views;
using LivetWPFChromeHelpDesk1.ViewModels;

namespace LivetWPFChromeHelpDesk1.ViewModels
{
    class ViewModel4 : ViewModel
    {
        #region 変更通知プロパティ
        //-----------------------------------------------
        public string txt本日 { get; set; }
        //-----------------------------------------------
        #endregion

        Window win = null;
        public ViewModel4()
        {
            Loaded = new Livet.Commands.ListenerCommand<Window>((w) =>
            {
                if (NeedHideOwner && w.Owner != null && w.Owner.Visibility == Visibility.Visible)
                {
                    win = w;
                    //w.Owner.Hide();
                }
            });
            //Initialize()では表示されない
            txt本日 = Convert.ToString(DateTime.Today.ToShortDateString());
        }
        public bool NeedHideOwner { get; set; }
        public ICommand Loaded { get; private set; }

        public void Initialize()
        {
            if (win != null) win.Owner.Hide();
        }

        /// <summary>
        /// Command for opening the website.
        /// </summary>
        private RelayCommand<object> _OpenWebsiteCommand;
        public ICommand OpenWebsiteCommand
        {
            get
            {
                if (_OpenWebsiteCommand == null)
                {
                    _OpenWebsiteCommand = new RelayCommand<object>(OpenWebsite);
                }

                return _OpenWebsiteCommand;
            }
        }
        private void OpenWebsite(object url)
        {
            System.Diagnostics.Process.Start(url as string);
        }
        /* RelayCommandの時は下記でも可能
        private RelayCommand<object> _OpenSupportWebsiteCommand;
        private void OpenWebsite(object url)
        {
            System.Diagnostics.Process.Start(url as string);
        }
        */

        //--------------------------------------------------------
        ListenerCommand<string> _OpenWebsiteCommand2;
        public ListenerCommand<string> OpenWebsiteCommand2
        {
            get
            {
                if (_OpenWebsiteCommand2 == null)
                {
                    _OpenWebsiteCommand2 = new ListenerCommand<string>(OpenWebsite, () => true);
                }
                return _OpenWebsiteCommand2;
            }
        }
        void OpenWebsite(string str)
        {
            if (str == null)
            {
                Messenger.Raise(new InformationMessage("Cancel", "Error", MessageBoxImage.Error, "Info"));
                return;
            }

            if (str != null && str.Length > 0)
            {
                System.Diagnostics.Process.Start(str);
            }
        }
        //--------------------------------------------------------

        #region CloseCommand
        private ViewModelCommand _CloseCommand;
        public ViewModelCommand CloseCommand
        {
            get
            {
                if (_CloseCommand == null)
                {
                    _CloseCommand = new ViewModelCommand(Close);
                }
                return _CloseCommand;
            }
        }
        public void Close()
        {
            var window = System.Windows.Application.Current.Windows.OfType<Window>().SingleOrDefault((w) => w.IsActive);
            window.Close();
        }
        #endregion

    }
}