■ XmlDataProvider 엘리먼트 사용하기

----------------------------------------------------------------------------------------------------

HelpContents.xml

 

<HelpContents xmlns="">

    <Topic Header="Copyright Information" Source="/Help/Copyright.xaml" />

    <Topic Header="Program Overview" Source="/Help/Overview.xaml" />

    <Topic Header="The Menu">

        <Topic Header="The File Menu" Source="/Help/FileMenu.xaml" />

        <Topic Header="The Help Menu" Source="/Help/HelpMenu.xaml" />

    </Topic>

</HelpContents>

----------------------------------------------------------------------------------------------------

 

■ XmlDataProvider 엘리먼트 사용하기

----------------------------------------------------------------------------------------------------

HelpWindow.xaml

 

<Window

    x:Class="DS.Core.WPF.UI.HelpWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    ShowInTaskbar="False"

    WindowStartupLocation="CenterScreen"

    Width="800"

    Height="600"

    Title="도움말">

    <Window.Resources>

        <HierarchicalDataTemplate

            DataType="Topic"

            ItemsSource="{Binding XPath=Topic}">

            <StackPanel Orientation="Horizontal">

                <Image

                    Name="image"

                    Margin="2"

                    Stretch="None"

                    Source="QuestionMark.png" />

                <TextBlock

                    VerticalAlignment="Center"

                    FontSize="12pt"

                    Text="{Binding XPath=@Header}" />

            </StackPanel>

            <HierarchicalDataTemplate.Triggers>

                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}, Path=HasItems}" Value="True">

                    <Setter

                        TargetName="image"

                        Property="Image.Source"

                        Value="ClosedBook.png" />

                </DataTrigger>

                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}, Path=IsExpanded}" Value="True">

                    <Setter

                        TargetName="image"

                        Property="Image.Source"

                        Value="OpenBook.png" />

                </DataTrigger>

            </HierarchicalDataTemplate.Triggers>

        </HierarchicalDataTemplate>

    </Window.Resources>

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="30*" />

            <ColumnDefinition Width="Auto" />

            <ColumnDefinition Width="70*" />

        </Grid.ColumnDefinitions>

        <TreeView

            Name="treeView"

            Grid.Column="0"

            SelectedValuePath="@Source"

            SelectedItemChanged="treeView_SelectedItemChanged" />

        <GridSplitter

            Grid.Column="1"

            HorizontalAlignment="Center"

            VerticalAlignment="Stretch"

            Width="6" />

        <Frame

            Name="frame"

            Grid.Column="2"

            Navigated="frame_Navigated" />

    </Grid>

</Window>

 

HelpWindow.xaml.cs

 

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Navigation;

using System.Xml;

 

namespace DS.Core.WPF.UI

{

    /// <summary>

    /// 도움말 윈도우

    /// </summary>

    public partial class HelpWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - HelpWindow(helpContentsUri)

 

        /// <summary>

        /// 생성자

        /// </summary>

        /// <param name="helpContentsUri">도움말 목차 URI</param>

        public HelpWindow(string helpContentsUri)

        {

            InitializeComponent();

 

            XmlDataProvider xmlDataProvider = new XmlDataProvider();

 

            xmlDataProvider.Source = new Uri(helpContentsUri, UriKind.RelativeOrAbsolute);

 

            xmlDataProvider.XPath = "HelpContents";

 

            Binding binding = new Binding();

 

            binding.Source = xmlDataProvider;

            binding.XPath  = "Topic";

 

            this.treeView.SetBinding(TreeView.ItemsSourceProperty, binding);

 

            this.treeView.Focus();

        }

 

        #endregion

 

        #region 생성자 - HelpWindow(helpContentsUri, documentUri)

 

        /// <summary>

        /// 도움말

        /// </summary>

        /// <param name="helpContentsUri">도움말 목차 URI</param>

        /// <param name="documentUri">문서 URI</param>

        public HelpWindow(string helpContentsUri, string documentUri) : this(helpContentsUri)

        {

            if(documentUri != null)

            {

                this.frame.Source = new Uri(documentUri, UriKind.RelativeOrAbsolute);

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 트리 뷰 선택 항목 변경시 처리하기 - treeView_SelectedItemChanged(sender, e)

 

        /// <summary>

        /// 트리 뷰 선택 항목 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)

        {

            if(this.treeView.SelectedValue != null)

            {

                this.frame.Source = new Uri(this.treeView.SelectedValue as string, UriKind.RelativeOrAbsolute);

            }

        }

 

        #endregion

 

        #region 프레임 이동시 처리하기 - frame_Navigated(sender, e)

 

        /// <summary>

        /// 프레임 이동시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void frame_Navigated(object sender, NavigationEventArgs e)

        {

            if(e.Uri != null && e.Uri.OriginalString != null && e.Uri.OriginalString.Length > 0)

            {

                SelectItem(this.treeView, e.Uri.OriginalString);

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 항목 선택하기 - SelectItem(itemsControl, documentUri)

 

        /// <summary>

        /// 항목 선택하기

        /// </summary>

        /// <param name="itemsControl">목록 컨트롤</param>

        /// <param name="documentUri">문서 URI</param>

        /// <returns>처리 결과</returns>

        private bool SelectItem(ItemsControl itemsControl, string documentUri)

        {

            foreach(object item in itemsControl.Items)

            {

                XmlElement xmlElement = item as XmlElement;

 

                string sourceAttribute = xmlElement.GetAttribute("Source");

 

                TreeViewItem treeViewItem = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

 

                if(sourceAttribute != null && sourceAttribute.Length > 0 && documentUri.EndsWith(sourceAttribute))

                {

                    if(treeViewItem != null && !treeViewItem.IsSelected)

                    {

                        treeViewItem.IsSelected = true;

                    }

 

                    return true;

                }

 

                if(treeViewItem != null)

                {

                    bool isExpanded = treeViewItem.IsExpanded;

 

                    treeViewItem.IsExpanded = true;

 

                    if(treeViewItem.HasItems && SelectItem(treeViewItem, documentUri))

                    {

                        return true;

                    }

 

                    treeViewItem.IsExpanded = isExpanded;

                }

            }

 

            return false;

        }

 

        #endregion

    }

}

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요