본문 바로가기
C#/WPF

Binding - UpdateSourceTrigger

by Falto 2023. 3. 1.
<Window x:Class="IamNamespace.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Height="450" Width="450" Background="#999999">
    <Grid>
        <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Title}"/>
    </Grid>
</Window>

xaml을 보면 Window의 Title에 TextBox의 Text를 바인딩함으로써, 텍스트 박스에 글자를 입력하면 실시간으로 윈도우의 제목이 바뀔 것처럼 보인다. 하지만 실제로 실행시켜보면...

글자를 아무리 써도 제목이 비어 있는 상태다. 이는 UpdateSourceTrigger가 TextBox의 Focus를 잃을 때로 되어있기 때문이다.

<Window x:Class="IamNamespace.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Height="450" Width="450" Background="#999999">
    <Grid>
        <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Title, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>​

UpdateSourceTrigger=PropertyChanged를 추가해주면 속성이 바뀔 때마다 Update를 해서

글자를 입력함과 동시에 제목이 바뀌는 것을 볼 수 있다.

'C# > WPF' 카테고리의 다른 글

OneWay와 TwoWay  (0) 2023.03.01
RelativeSource Binding  (0) 2023.03.01
DataContext Binding  (0) 2023.03.01
[C# WPF] Binding  (0) 2023.02.18
[C# WPF] super mario 63의 cheat를 구현해 보자.  (0) 2023.02.03

댓글