[WPF] WPF PriorityBinding

C#/WPF 2023. 3. 17. 14:11
728x90
728x170

PriorityBinding 은 바인딩 시 한 곳에 여러 필드를 우선순위에 따라 나타내고자 할 때 쓰입니다.

예를 들면 어떤 검사 결과 값을 표시할때 처음에는 검사중 - 결과 산출중 - 결과 를 표시할 때 사용할 수 있습니다.

AsyncSampleData.cs

using System.Threading;

namespace Wpf.Test
{
    public class AsyncSampleData
    {
        private string fastValue;
        private string slowerValue;
        private string slowestValue;

        public string FastValue
        {
            get { return fastValue; }
            set { fastValue = value; }
        }

        public string SlowerValue
        {
            get
            {
                Thread.Sleep(3000);

                return this.slowerValue;
            }
            set
            {
                this.slowerValue = value;
            }
        }

        public string SlowestValue
        {
            get
            {
                Thread.Sleep(5000);

                return this.slowestValue;
            }
            set
            {
                this.slowestValue = value;
            }
        }
    }
}

FastValue 가 처음에 표시될 값이며 그다음으로 SlowerValue, SlowestValue 값이 표시되도록 Thread.Sleep을 하였습니다.

MainWindow.xaml

<Window
    x:Class="Wpf.Test.MainWindow"
    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:local="clr-namespace:Wpf.Test"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Window.Resources>
        <local:AsyncSampleData
            x:Key="AsyncData"
            FastValue="검사중..(Fast Value)"
            SlowerValue="결과 산출중..(Slower Value)"
            SlowestValue="결과(합격!) (Slowest Value)" />
    </Window.Resources>

    <StackPanel>
        <StackPanel
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            DataContext="{Binding Source={StaticResource AsyncData}}">
            <TextBlock
                Margin="10"
                HorizontalAlignment="Center"
                FontSize="18"
                FontWeight="Bold">
                Priority Binding
            </TextBlock>
            <TextBlock
                Width="200"
                HorizontalAlignment="Center"
                Background="LightGray"
                FontWeight="Bold"
                TextAlignment="Center">
                <TextBlock.Text>
                    <PriorityBinding FallbackValue="Default Value">
                        <Binding IsAsync="True" Path="SlowestValue" />
                        <Binding IsAsync="True" Path="SlowerValue" />
                        <Binding Path="FastValue" />
                    </PriorityBinding>
                </TextBlock.Text>
            </TextBlock>
        </StackPanel>
    </StackPanel>
</Window>

MainWindow.xaml.cs

using System.Windows;

namespace Wpf.Test
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}


결과
 

[Source]

https://github.com/kei-soft/KJunBlog/tree/master/Wpf.Test

 

GitHub - kei-soft/KJunBlog

Contribute to kei-soft/KJunBlog development by creating an account on GitHub.

github.com


참고
https://learn.microsoft.com/ko-kr/dotnet/desktop/wpf/data/how-to-implement-prioritybinding?view=netframeworkdesktop-4.8

 

728x90
그리드형
Posted by kjun
,