간단한 컨텍스트 바인딩 하기

드롭다운 개체에 의해서 하위 컨트롤의 데이터 값이 변경됩니다.

image

 

image

 

데이터로 사용된 개체

    public class Customer
    {
        public string Name { get; set; }
        public List<Order> Orders { get; set; }

        public override string ToString()
        {
            return this.Name;
        }
        
        public static List<Customer> CreateCustomers()
        {
            return new List<Customer>
            {
                new Customer 
                {
                    Name="Customer 1",
                    Orders=new List<Order>
                    {
                        new Order
                        {
                            Desc="Big Order",
                            OrderDetails=new List<OrderDetail>
                            {
                                new OrderDetail { Product="Glue", Quantity=21 },
                                new OrderDetail { Product="Fudge", Quantity=32 }
                            }
                        },
                        new Order
                        {
                            Desc="Little Order",
                            OrderDetails=new List<OrderDetail>
                            {
                                new OrderDetail { Product="Ham", Quantity=1 },
                                new OrderDetail { Product="Yarn", Quantity=2 }
                            }
                        }
                    }
                },
                new Customer 
                {
                    Name="Customer 2",
                    Orders=new List<Order>
                    {
                        new Order
                        {
                            Desc="First Order",
                            OrderDetails=new List<OrderDetail>
                            {
                                new OrderDetail { Product="Mousetrap", Quantity=4 }
                            }
                        }
                    }
                },
            };
        }
    }

 

MainWindow.xaml

<Window x:Class="TestWpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <ComboBox 
        IsSynchronizedWithCurrentItem="True" 
        ItemsSource="{Binding Path=.}" Margin="0,35,0,255" />
        <Label Content="{Binding Path=Name}" Height="34" HorizontalAlignment="Left" Margin="12,120,0,0" Name="label1" VerticalAlignment="Top" Width="195" />
        <ComboBox ItemsSource="{Binding Path=Orders}" Margin="12,189,296,101" />
        <Label Content="컨텍스트 개체" Height="28" HorizontalAlignment="Left" Margin="0,12,0,0" Name="label2" VerticalAlignment="Top" />
        <Label Content="현재의 컨텍스트 개체안에 (Name)항목" Height="28" HorizontalAlignment="Left" Margin="12,86,0,0" Name="label3" VerticalAlignment="Top" Width="195" />
        <Label Content="현재의 컨텍스트 개체안에 (Orders)항목리스트" Height="28" HorizontalAlignment="Left" Margin="12,160,0,0" Name="label4" VerticalAlignment="Top" Width="309" />
        <Label Content="드롭다운하면 현재의 개체에 맞게 아래데이터가 변경된다." Height="28" HorizontalAlignment="Left" Margin="0,52,0,0" Name="label5" VerticalAlignment="Top" />
    </Grid>
</Window>

MainWindow.xaml.cs

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;
using System.Diagnostics;

namespace TestWpfApplication
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = DataLibrary.Customer.CreateCustomers();

        }


    }
}

 

Selector.IsSynchronizedWithCurrentItem

http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=KO-KR&k=k(SYSTEM.WINDOWS.CONTROLS.PRIMITIVES.SELECTOR.ISSYNCHRONIZEDWITHCURRENTITEM);k(VS.XAMLEDITOR);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22)&rd=true

 

 

ex) 고객 주문의 상세주문을 보고싶다면?

image
Oders 아래의 데이터를 가지고 오고 싶다면

image

이렇게 구현해주면된다.

+ Recent posts