간단한 컨텍스트 바인딩 하기
드롭다운 개체에 의해서 하위 컨트롤의 데이터 값이 변경됩니다.
데이터로 사용된 개체
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
ex) 고객 주문의 상세주문을 보고싶다면?
이렇게 구현해주면된다.
'WPF' 카테고리의 다른 글
| [DataGrid/Element Property Binding] 선택한 행 정보받아오기 (0) | 2010.07.09 |
|---|---|
| [OneTime, OneWayToSource, TwoWay]바인딩 속성 (0) | 2010.07.08 |
| Microsoft Silverlight Media Framework (0) | 2010.07.08 |
| [CollectionViewSource] DataGird 사용하기 (0) | 2010.07.07 |
| [Event/Property/Triggers]컨트롤 포커스 받았을때 스타일변경 (0) | 2010.07.01 |
| [Drag and Drop Objects] 개체 드래그 하기 (0) | 2010.06.23 |
| [ResourceDictionary]동적으로 어플리케이션 리소스 적용하기 (0) | 2010.06.23 |
| [Resource]Width/Height 치수를 리소스로 설정 (0) | 2010.06.22 |
| [DesignHeight/DesignWidth] 디자인 타임에 크기설정 (0) | 2010.06.17 |
| [Silverlight 4 Documentation/Tools] 도움말다운로드/툴킷 (0) | 2010.06.17 |