2021년 11월 21일 일요일

WPF 데이터바인딩 교육동영상, Source, StaticResource를 통한 데이터 바인딩

 

WPF 데이터바인딩 교육동영상, Source, StaticResource를 통한 데이터 바인딩


https://www.youtube.com/watch?v=Orwyaq51MXQ&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=14&t=2s 


https://www.youtube.com/watch?v=wgz3OyVqfY4&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=13&t=1s 

  • FrameworkElement를 상속받지 않은 객체를 바인딩 하기 위해서는 StaticResource를 사용하여 데이터 바인딩을 할 수 있는데 XAML의 Resource절에 객체를 정의하고 StaticResource로 접근하면 된다.

<Window>

<Window.Resources>

       <src:ClockTicker1 x:Key="clock" />

</Window.Resources>

<Window.Content>

       <Binding Source="{StaticResource clock}" Path="DateTime" />

</Window.Content>

</Window>

Window.Resource 절에서 ClockTicker1을 정의하고 clock 이라는 key를 정의했으며 이 키를 이용하여 Binding 절에서 Source, StaticResource를 사용하여 지정하고 ColckTicker1의 DateTime 프로퍼티와 바인딩 시켰다.

  • 전자시계를 만들어 보는데 현재 시각이 변했을 때 통지하는 메커니즘이 필요할 것이다.
  • DigitalClock 이라는 이름으로 WPF 응용프로그램 프로젝트를 생성 
  • ClockTicker1.cs

//---------------------------------------------

// ClockTicker1.cs (c) by Charles Petzold

//---------------------------------------------

using System;

using System.Windows;

using System.Windows.Threading;


namespace DigitalClock

{

    public class ClockTicker1 : DependencyObject

    {

        // 의존 프로퍼티 선언

        public static DependencyProperty DateTimeProperty =

                DependencyProperty.Register("DateTime"typeof(string),

                                            typeof(ClockTicker1));


        // CLR property로 의존 프로퍼티를 노출

        public string DateTime

        {

            set { SetValue(DateTimeProperty, value); }

            get { return (DateTime)GetValue(DateTimeProperty); }

        }


        // 생성자.

        public ClockTicker1()

        {

            DispatcherTimer timer = new DispatcherTimer();

            timer.Tick += TimerOnTick;

            timer.Interval = TimeSpan.FromSeconds(1);  //1초에 한번씩

            timer.Start();

        }


        // DateTime property 값을 설정하기 위한 Timer 이벤트 핸들러

        void TimerOnTick(object sender, EventArgs args)

        {

            DateTime = DateTime.Now.ToString(“yyyyMMdd HHmmss”);

        }

    }

}


  • MainWindow.xaml

<!-- =====================================================

      DigitalClockWindow.xaml (c) by Charles Petzold

     ===================================================== -->

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

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

        xmlns:src="clr-namespace:DigitalClock"

        Title="Digital Clock"

x:Class="DigitalClock.MainWindow"

        SizeToContent="WidthAndHeight"

        ResizeMode="CanMinimize"

        FontFamily="Bookman Old Style" 

        FontSize="36pt">

    <Window.Resources>

        <src:ClockTicker1 x:Key="clock"/>

    </Window.Resources>

    <Window.Content>

        <Binding Source="{StaticResource clock}" Path="DateTime" />

    </Window.Content>

</Window>

  • 실행결과



#StaticResource, #x:Static, #WPF데이터컨텍스트#데이터컨텍스트#WPF데이터바인딩#WPF교육#WPF강좌#WPF학원#DataBinding#WPFDataBinding

StaticResource, x:Static,WPF데이터컨텍스트, 데이터컨텍스트,데이터바인딩, WPF데이터바인딩, WPF교육, WPF강좌, WPF학원, DataBinding, WPFDataBinding

댓글 없음:

댓글 쓰기

(C#교육동영상)C# ADO.NET 실습 ODP.NET/ODAC 설치 오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원

  (C#교육동영상)C# ADO.NET 실습  ODP.NET/ODAC 설치  오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원 https://www.youtube.com/watch?v=qIPU85yAlzc&list=PLxU-i...