잠토의 잠망경

DataBinding simple 1 converter 본문

공부/WPF

DataBinding simple 1 converter

잠수함토끼 2015. 3. 24. 22:41








<Window x :Class="WpfApplication28.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local ="clr-namespace:WpfApplication28"
        Title="MainWindow" Height ="350" Width="525">

    <Window.Resources >
        <ResourceDictionary>
            <local: Person x: Key="Test1" Name ="hi" Age="26"/>
            <local: Person x: Key="Test2" />

            <local: Person x: Key="Test3" />
            <local: AgeToForegroundConverter x: Key="ageConverter"/>
            <local: wStringToInt x: Key="sStringToInt"/>

        </ResourceDictionary>
    </Window.Resources >
   
   
    <Grid >
        <StackPanel Orientation ="Vertical">

            <TextBox Text ="{Binding Path =Age,
                            Source={StaticResource Test1},
                            Mode=TwoWay
                            }" Width="100"/>

            <TextBox Width ="100"
                     Background="{  Binding Path=Age,
                                    Mode=TwoWay,
                                    Source={StaticResource Test1},
                                    Converter={StaticResource ageConverter}}" />


            <TextBox Width ="100"
                     Background="{  Binding Path=Age,
                                    Mode=TwoWay,
                                    Source={StaticResource Test2},
                                    Converter={StaticResource ageConverter}}" />


            <StackPanel DataContext ="{StaticResource Test3 }">
                <TextBlock Text ="{Binding Path =Name}"/>
                <TextBlock Text ="{Binding Path =Age}"/>
            </StackPanel>



        </StackPanel>


    </Grid >

       
</Window>

using System ;
using System .Collections.Generic;
using System .ComponentModel;
using System .Linq;
using System .Text;
using System .Windows;
using System .Windows.Controls;
using System .Windows.Data;
using System .Windows.Documents;
using System .Windows.Input;
using System .Windows.Media;
using System .Windows.Media.Imaging;
using System .Windows.Navigation;
using System .Windows.Shapes;

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


    public class Person : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged ;

        protected void Nofity( String propName )
        {
            if( null!= this. PropertyChanged)
            {
                PropertyChanged(this , new PropertyChangedEventArgs(propName));
            }
        }


        String name ;
        int age;


        public String Name
        {
            get { return this. name; }
            set
            {
                if( value != this. name)
                {
                    this. name = value;
                    Nofity( "Name");
                }
            }
        }


        public int Age
        {
            get { return this. age; }

            set
            {
                if( value != this. age)
                {
                    this. age = value;
                    Nofity( "Age");
                }
            }
        }


    }


    public class wStringToInt:IValueConverter
    {

        public object Convert( object value, Type targetType , object parameter , System.Globalization .CultureInfo culture)
        {
            if( targetType != typeof (String))
            {
                return null;
            }

            return int. Parse(value .ToString());
        }

        public object ConvertBack(object value , Type targetType, object parameter, System.Globalization .CultureInfo culture)
        {
            return null;
        }
    }



    public class AgeToForegroundConverter: IValueConverter
    {
        public object Convert( object value, Type targetType , object parameter , System.Globalization .CultureInfo culture)
        {
 
            if ( targetType != typeof (Brush))
            {
                return null;
            }
            //else if (targetType == typeof(String))
            {
                int age = int. Parse(value .ToString());

                return ( age > 25 ? Brushes.Red : Brushes.Black);
            }
 



        }

        public object ConvertBack(object value , Type targetType, object parameter, System.Globalization .CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}





using System ;
using System .Collections.Generic;
using System .ComponentModel;
using System .Linq;
using System .Text;
using System .Windows;
using System .Windows.Controls;
using System .Windows.Data;
using System .Windows.Documents;
using System .Windows.Input;
using System .Windows.Media;
using System .Windows.Media.Imaging;
using System .Windows.Navigation;
using System .Windows.Shapes;

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


    public class Person : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged ;

        protected void Nofity( String propName )
        {
            if( null!= this. PropertyChanged)
            {
                PropertyChanged(this , new PropertyChangedEventArgs(propName));
            }
        }


        String name ;
        int age;


        public String Name
        {
            get { return this. name; }
            set
            {
                if( value != this. name)
                {
                    this. name = value;
                    Nofity( "Name");
                }
            }
        }


        public int Age
        {
            get { return this. age; }

            set
            {
                if( value != this. age)
                {
                    this. age = value;
                    Nofity( "Age");
                }
            }
        }


    }


    public class wStringToInt:IValueConverter
    {

        public object Convert( object value, Type targetType , object parameter , System.Globalization .CultureInfo culture)
        {
            if( targetType != typeof (String))
            {
                return null;
            }

            return int. Parse(value .ToString());
        }

        public object ConvertBack(object value , Type targetType, object parameter, System.Globalization .CultureInfo culture)
        {
            return null;
        }
    }

    public class WStringToHex:IValueConverter
    {

        public object Convert( object value, Type targetType , object parameter , System.Globalization .CultureInfo culture)
        {
            return ( int. Parse(value .ToString())).ToString( "x");
        }

        public object ConvertBack(object value , Type targetType, object parameter, System.Globalization .CultureInfo culture)
        {
            return int. Parse((String )value , System.Globalization .NumberStyles.HexNumber);
        }
    }




    public class AgeToForegroundConverter: IValueConverter
    {
        public object Convert( object value, Type targetType , object parameter , System.Globalization .CultureInfo culture)
        {
 
            if ( targetType != typeof (Brush))
            {
                return null;
            }
            //else if (targetType == typeof(String))
            {
                int age = int. Parse(value .ToString());

                return ( age > 25 ? Brushes.Red : Brushes.Black);
            }
 



        }

        public object ConvertBack(object value , Type targetType, object parameter, System.Globalization .CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

<Window x :Class="WpfApplication28.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local ="clr-namespace:WpfApplication28"
        Title="MainWindow" Height ="350" Width="525">

    <Window.Resources >
        <ResourceDictionary>
            <local: Person x: Key="Test1" Name ="hi" Age="26"/>
            <local: Person x: Key="Test2" />

            <local: Person x: Key="Test3" />
            <local: AgeToForegroundConverter x: Key="ageConverter"/>
            <local: wStringToInt x: Key="sStringToInt"/>
            <local: WStringToHex x: Key="sTringToHex"/>

        </ResourceDictionary>
    </Window.Resources >
   
   
    <Grid >
        <StackPanel Orientation ="Vertical">

            <TextBox Text ="{Binding Path =Age,
                            Source={StaticResource Test1},
                            Mode=TwoWay,
                Converter={StaticResource sTringToHex}
                            }" Width="100"/>

            <TextBox Width ="100"
                     Background="{  Binding Path=Age,
                                    Mode=TwoWay,
                                    Source={StaticResource Test1},
                                    Converter={StaticResource ageConverter}}" />


            <TextBox Width ="100"
                     Background="{  Binding Path=Age,
                                    Mode=TwoWay,
                                    Source={StaticResource Test2},
                                    Converter={StaticResource ageConverter}}" />


            <StackPanel DataContext ="{StaticResource Test3 }">
                <TextBlock Text ="{Binding Path =Name}"/>
                <TextBlock Text ="{Binding Path =Age}"/>
            </StackPanel>



        </StackPanel>


    </Grid >
       
</Window>






data변경과 동시에 즉시 적용된다.



<Window x :Class="WpfApplication28.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local ="clr-namespace:WpfApplication28"
        Title="MainWindow" Height ="350" Width="525">

    <Window.Resources >
        <ResourceDictionary>
            <local: Person x: Key="Test1" Name ="hi" Age="26"/>
            <local: Person x: Key="Test2" />

            <local: Person x: Key="Test3" />
            <local: AgeToForegroundConverter x: Key="ageConverter"/>
            <local: wStringToInt x: Key="sStringToInt"/>
            <local: WStringToHex x: Key="sTringToHex"/>

        </ResourceDictionary>
    </Window.Resources >
   
   
    <Grid >
        <StackPanel Orientation ="Vertical">

            <TextBox x :Name="txtAge"
                        Text="{Binding Path=Age,
                            Source={StaticResource Test1},
                            Mode=TwoWay, 
                            UpdateSourceTrigger=PropertyChanged
                            }" Width="100"/>

            <TextBox Width ="100"
                     Background="{  Binding Path=Age,
                                    Mode=TwoWay,
                                    Source={StaticResource Test1},
                                    Converter={StaticResource ageConverter}}" />


            <TextBox Width ="100"
                     Background="{  Binding Path=Age,
                                    Mode=TwoWay,
                                    Source={StaticResource Test2},
                                    Converter={StaticResource ageConverter}}" />


            <StackPanel DataContext ="{StaticResource Test3 }">
                <TextBlock Text ="{Binding Path =Name}"/>
                <TextBlock Text ="{Binding Path =Age}"/>
            </StackPanel>



        </StackPanel>


    </Grid >
       
</Window>








<Window x :Class="WpfApplication28.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local ="clr-namespace:WpfApplication28"
        Title="MainWindow" Height ="350" Width="525">

    <Window.Resources >
        <ResourceDictionary>
            <local: Person x: Key="Test1" Name ="hi" Age="216"/>
            <local: Person x: Key="Test2" />

            <local: Person x: Key="Test3" />
            <local: AgeToForegroundConverter x: Key="ageConverter"/>
            <local: wStringToInt x: Key="sStringToInt"/>
            <local: WStringToHex x: Key="sTringToHex"/>

        </ResourceDictionary>
    </Window.Resources >
   
   
    <Grid >
        <StackPanel Orientation ="Vertical">

            <TextBlock>
                <TextBlock Text ="{Binding Path =Name,Source={ StaticResource Test1}}"/>
                (Age: <TextBlock Text ="{Binding Path =Age, Source={ StaticResource Test1}}"
                                 Background="{Binding Path=Age, Source={StaticResource Test1 },
                                                Converter={StaticResource ageConverter},
                                                UpdateSourceTrigger=PropertyChanged}"/> )
            </TextBlock>


        </StackPanel>


    </Grid >
       
</Window>






<Window x :Class="WpfApplication28.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local ="clr-namespace:WpfApplication28"
        Title="MainWindow" Height ="350" Width="525">

    <Window.Resources >
        <ResourceDictionary>
            <Style TargetType ="{x: Type Button}">
                <Setter Property ="Width" Value="100"/>
                <Setter Property ="Height" Value="100"/>
                <Setter Property ="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Rectangle Fill ="Orange"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
               
            </Style>
        </ResourceDictionary>
    </Window.Resources >
   
   
    <Grid >
        <StackPanel Orientation ="Vertical">
               
                <Button Content ="TEST"/>

        </StackPanel>


    </Grid >
       
</Window>





<Window x :Class="WpfApplication28.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local ="clr-namespace:WpfApplication28"
        Title="MainWindow" Height ="350" Width="525">

    <Window.Resources >
        <ResourceDictionary>
            <Style TargetType ="{x: Type Button}">
                <Setter Property ="Width" Value="100"/>
                <Setter Property ="Height" Value="100"/>
                <Setter Property ="Background" Value="Blue"/>
                <Setter Property ="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Rectangle Fill ="{TemplateBinding Property=Background}"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
               
            </Style>
        </ResourceDictionary>
    </Window.Resources >
   
   
    <Grid >
        <StackPanel Orientation ="Vertical">
               
                <Button Content ="TEST"/>

        </StackPanel>


    </Grid >
       
</Window>





<Window x :Class="WpfApplication28.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local ="clr-namespace:WpfApplication28"
        Title="MainWindow" Height ="350" Width="525">

    <Window.Resources >
        <ResourceDictionary>
            <Style TargetType ="{x: Type Button}">
                <Setter Property ="Width" Value="100"/>
                <Setter Property ="Height" Value="100"/>
                <Setter Property ="Background" Value="Blue"/>
                <Setter Property ="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Rectangle Fill ="{Binding Path =Background, RelativeSource={RelativeSource TemplatedParent }}"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
               
            </Style>
        </ResourceDictionary>
    </Window.Resources >
   
   
    <Grid >
        <StackPanel Orientation ="Vertical">
               
                <Button Content ="TEST"/>

        </StackPanel>


    </Grid >
       
</Window>


<Window x :Class="WpfApplication28.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local ="clr-namespace:WpfApplication28"
        Title="MainWindow" Height ="350" Width="525">

    <Window.Resources >
        <ResourceDictionary>
           
            <ControlTemplate x :Key="ButtonTemplate">
                <Grid>
                    <Rectangle Fill ="{Binding Path =Background, RelativeSource={RelativeSource TemplatedParent }}"/>
                </Grid>
               
            </ControlTemplate>
           
           
            <Style TargetType ="{x: Type Button}">
                <Setter Property ="Width" Value="100"/>
                <Setter Property ="Height" Value="100"/>
                <Setter Property ="Background" Value="Blue"/>
                <Setter Property ="Template" Value="{ StaticResource ButtonTemplate}"/>



            </Style>
        </ResourceDictionary>
    </Window.Resources >
   
   
    <Grid >
        <StackPanel Orientation ="Vertical">
               
                <Button Content ="TEST"/>

        </StackPanel>


    </Grid >
       
</Window>


Comments