WPF & C# - 바인딩 모드별 코드 예제 ( Binding Mode / Default / OneTime / OneWay / OneWayTosource / TwoWay )

2018. 8. 3. 09:59Programing (프로그래밍)/WPF & C# (C Sharp)

728x90
반응형


 WPF & C# - 바인딩 모드별 코드 예제 ( Binding Mode / Default / OneTime / OneWay / OneWayTosource / TwoWay )



binding.zip




MainWindow.xaml.cs



1
2
3
4
5
6
7
8
9
<StackPanel Margin="10" Background="#FFFFFFAF">
    <Label x:Name="lblMode" Content="Label" Height="30" HorizontalAlignment="Center"/>
    <Button x:Name="btn0" Content="Binding" Click="btn0_Click" Height="30" Margin="5"/>
    <StackPanel Orientation="Horizontal" Height="30" Margin="5">
        <Button x:Name="btn1" Content=" + 1 " Click="btn1_Click"/>
        <Label x:Name="lbl" Content="Label"/>
    </StackPanel>
    <Slider x:Name="slider" Height="30" Margin="5"/>
</StackPanel>
cs



MainWindow.xaml.cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int i = 0;
private void btn0_Click(object sender, RoutedEventArgs e)
{
    i++;
    Binding binding = new Binding();
    binding.Source = slider;   // 슬라이더
    binding.Path = new PropertyPath("Value");   // 슬라이더의 프로퍼티
    if (i % 5 == 0) binding.Mode = BindingMode.Default;
    if (i % 5 == 1) binding.Mode = BindingMode.OneTime;
    if (i % 5 == 2) binding.Mode = BindingMode.OneWay;
    if (i % 5 == 3) binding.Mode = BindingMode.OneWayToSource;
    if (i % 5 == 4) binding.Mode = BindingMode.TwoWay;
    lbl.SetBinding(ContentProperty, binding);

    lblMode.Content = binding.Mode;
}
 
private void btn1_Click(object sender, RoutedEventArgs e)
{
    if(lbl.Content != null) lbl.Content = (double)lbl.Content + 1;
}
cs



참고하면 좋을 사이트


728x90
반응형