기본적인 Xaml 구문입니다. NavigateUrl 은 Link 연결할 Url 값을 저정하고 TargetName는 Url을 Calll 할때 웹브라이저를 여는 프로퍼티입니다.
- TargetName = _blank, _media, _search = 창을 새로 뛰울때
- TargetName = _parent, _self, _top, “” = 기존창에서 열때
<HyperlinkButton x:Name="hyperBlog" NavigateUri="http://www.taedi.kr
TargetName="_blank" "/>
HyperlinkButton 기능구현 방법
Xaml
<UserControl x:Class="ControlTest10.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="260" Height="160">
<Grid x:Name="LayoutRoot" Background="White">
<HyperlinkButton Margin="17,19,0,0" VerticalAlignment="Top" Height="23"
Content="Naver" x:Name="hyperNamver"
HorizontalAlignment="Left" Width="113"
NavigateUri="http://www.naver.com" TargetName="_blank" />
<HyperlinkButton Margin="17,49,0,0" x:Name="hyperDaum" Content="Daum"
Width="113" HorizontalAlignment="Left"
NavigateUri="http://www.daum.com" TargetName="_blank"
Height="23" VerticalAlignment="Top"/>
<HyperlinkButton Margin="16,78,0,59" x:Name="hyperGoogle" Content="Google"
HorizontalAlignment="Left" Width="113"
NavigateUri="http://www.google.co.kr" TargetName="_blank"/>
<HyperlinkButton Margin="17,0,0,30" x:Name="hyperBlog"
Content="Blog" HorizontalAlignment="Left" Width="113"
NavigateUri="http://www.taedi.kr" TargetName="_blank"
VerticalAlignment="Bottom" Height="23"/>
</Grid>
</UserControl>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace ControlTest10
{
public partial class Page : UserControl
{
private HyperlinkButton hyperNaver;
private HyperlinkButton hyperDaum;
private HyperlinkButton hyperGoogle;
private HyperlinkButton hyperBlog;
public Page()
{
InitializeComponent();
hyperNaver = new HyperlinkButton();
hyperDaum = new HyperlinkButton();
hyperGoogle = new HyperlinkButton();
hyperBlog = new HyperlinkButton();
hyperNaver.Click += new RoutedEventHandler(hyperNaver_Click);
hyperDaum.Click += new RoutedEventHandler(hyperDaum_Click);
hyperGoogle.Click += new RoutedEventHandler(hyperGoogle_Click);
hyperBlog.Click += new RoutedEventHandler(hyperBlog_Click);
hyperNaver.Margin = new Thickness(17, 0, 0, 30);
hyperNaver.Content = "Naver";
hyperNaver.Height = 27;
hyperNaver.Width = 113;
hyperNaver.VerticalAlignment = VerticalAlignment.Top;
hyperNaver.HorizontalAlignment = HorizontalAlignment.Left;
hyperDaum.Margin = new Thickness(17, 25, 0, 0);
hyperDaum.Content = "Daum";
hyperDaum.Height = 27;
hyperDaum.Width = 113;
hyperDaum.VerticalAlignment = VerticalAlignment.Top;
hyperDaum.HorizontalAlignment = HorizontalAlignment.Left;
hyperGoogle.Margin = new Thickness(17, 50, 0, 0);
hyperGoogle.Content = "Google";
hyperGoogle.Height = 27;
hyperGoogle.Width = 113;
hyperGoogle.VerticalAlignment = VerticalAlignment.Top;
hyperGoogle.HorizontalAlignment = HorizontalAlignment.Left;
hyperBlog.Margin = new Thickness(16, 75, 0, 59);
hyperBlog.Content = "Blog";
hyperBlog.Height = 27;
hyperBlog.Width = 113;
hyperBlog.VerticalAlignment = VerticalAlignment.Top;
hyperBlog.HorizontalAlignment = HorizontalAlignment.Left;
this.LayoutRoot.Children.Add(hyperNaver);
this.LayoutRoot.Children.Add(hyperDaum);
this.LayoutRoot.Children.Add(hyperGoogle);
this.LayoutRoot.Children.Add(hyperBlog);
}
void hyperNaver_Click(object sender, RoutedEventArgs e)
{
hyperNaver.NavigateUri = new Uri("http://www.naver.com");
hyperNaver.TargetName = "_Blank";
}
void hyperDaum_Click(object sender, RoutedEventArgs e)
{
hyperDaum.NavigateUri = new Uri("http://www.daum.net");
hyperDaum.TargetName = "_Blank";
}
void hyperGoogle_Click(object sender, RoutedEventArgs e)
{
hyperGoogle.NavigateUri = new Uri("http://www.google.co.kr");
hyperGoogle.TargetName = "_Blank";
}
void hyperBlog_Click(object sender, RoutedEventArgs e)
{
hyperBlog.NavigateUri = new Uri("http://www.taedi.kr");
hyperBlog.TargetName = "_Blank";
}
}
}
HyperLinkButton 변수를 선언합니다. 그리고 Page() 생성자에서 HyperLinkButton 을 그리기 위한 각 프로퍼티를 선언하고, Click 이벤트를 선언합니다.
각각 선언된 이벤트안에는 특정 링크를 연결하고TargetName를 지정합니다.
Xaml로 코딩하는것 보다 C#에서 동적으로 HyperLinkButton 을 만드는 것이 코드량이 훨씬 많지요.. 이럴땐 다이렉트로 매번 같은 작업을 반복하는 것보다 사용자 정의 컽트롤로 HyperLinkButton을 만든다음 그것을 가져와 사용하는 편이 코드의 중복을 줄일 수 있고 효율적인 코딩작업을 할 수 있습니다
'.NET WPF' 카테고리의 다른 글
Silverlight 2 Released!! - x줄타게 기다렸습니다. (0) | 2008.10.14 |
---|---|
Silverlgith 2 Release (0) | 2008.10.14 |
Silverlight 2 Controls Review - Canvas (0) | 2008.10.13 |
Silverlight 2 Controls Review - ToggleButton (0) | 2008.10.08 |
Silverlight DeepZoom + Virtual Earth (0) | 2008.10.08 |
Silverlight 2 Controls Review - Slider (0) | 2008.10.07 |
Silverlight 2 RC0 버전 Fullscreen 테스트 (0) | 2008.10.06 |
댓글