WPF에서는 DataTemplateSelector가 기본적으로 제공되지만 윈폰은 없다 ㅡ.ㅡ;;
하지만 만들면 된다.
namespace GloryApp.Utils { public abstract class DataTemplateSelector : ContentControl { public virtual DataTemplate SelectTemplate(object item, DependencyObject container) { return null; } protected override void OnContentChanged(object oldContent, object newContent) { base.OnContentChanged(oldContent, newContent); ContentTemplate = SelectTemplate(newContent, this); } } }
이렇게 DataTemplateSelcotr class를 만들고
namespace GloryApp.Utils { public class ReserveTypeSelecter : DataTemplateSelector { public DataTemplate GeneralTrain { get; set; } public DataTemplate TransferTrain { get; set; } public override DataTemplate SelectTemplate(object item, DependencyObject container) { //var data = item as JrnyInfoModel; if (item == GeneralTrain) { return GeneralTrain; } else { return TransferTrain; } return base.SelectTemplate(item, container); } } }
이렇게 사용할 Selector를 하나만들고 SelectTemplate 메소드 안에서 자기가 원하는 DataTemplate를 선택해서 return 할수 있다.
<ListBox x:Name="ReserveListBox" ItemsSource="{Binding ResultViewData}" Grid.Row="1" > <ListBox.ItemTemplate> <DataTemplate> <st:ReserveTypeSelecter Content="{Binding}"> <st:ReserveTypeSelecter.GeneralTrain> <DataTemplate> <Grid> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding h_run_dt, Converter={StaticResource ShortDateFormatWithWeekConverter}}" Foreground="White" Margin="10,0,10,0"/> <StackPanel Margin="10,0,10,0" Width="50"> <TextBlock Text="{Binding h_trn_clsf_cd, Converter={StaticResource TrainNameConverter}}" Foreground="White" /> <TextBlock Text="{Binding h_trn_no}" Foreground="White" /> </StackPanel> <StackPanel Margin="10,0,10,0" Width="50" > <TextBlock Text="{Binding h_dpt_rs_stn_nm}" Foreground="White" /> <TextBlock Text="{Binding h_dpt_tm, Converter={StaticResource StringShortTimeConverter}}" Foreground="White" /> </StackPanel> <StackPanel Margin="10,0,10,0" Width="50"> <TextBlock Text="{Binding h_arv_rs_stn_nm}" Foreground="White" /> <TextBlock Text="{Binding h_arv_tm, Converter={StaticResource StringShortTimeConverter}}" Foreground="White" /> </StackPanel> <TextBlock Text="{Binding LimitDate}" Foreground="red" Margin="10,0,10,0"/> </StackPanel> </Grid> </DataTemplate> </st:ReserveTypeSelecter.GeneralTrain> <st:ReserveTypeSelecter.TransferTrain> <DataTemplate> <Grid> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding h_run_dt, Converter={StaticResource ShortDateFormatWithWeekConverter}}" Foreground="White" Margin="10,0,10,0"/> <StackPanel Margin="10,0,10,0" Width="50"> <TextBlock Text="{Binding h_trn_clsf_cd, Converter={StaticResource TrainNameConverter}}" Foreground="White" /> <TextBlock Text="{Binding h_trn_no}" Foreground="White" /> </StackPanel> <StackPanel Margin="10,0,10,0" Width="50" > <TextBlock Text="{Binding h_dpt_rs_stn_nm}" Foreground="White" /> <TextBlock Text="{Binding h_dpt_tm, Converter={StaticResource StringShortTimeConverter}}" Foreground="White" /> </StackPanel> </StackPanel> </Grid> </DataTemplate> </st:ReserveTypeSelecter.TransferTrain> </st:ReserveTypeSelecter> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
이제 xaml에서는 이렇게 2개의 DataTemplate을 넣어준다.
그래서 선택할수 있다.
'Windows Phone 7' 카테고리의 다른 글
[wp7] UserControl을 ViewModel처럼 Property Binding (0) | 2012.01.03 |
---|---|
[wp7] ListBox 선택했을때 Style 바꾸기 (0) | 2011.12.28 |
[wp7] 원단위 절사 하기 (340원을 300원으로) (0) | 2011.12.16 |
[wp7] 화페 단위 변환하기 (0) | 2011.12.13 |
[wp7] Custom UserControl 바인딩을 쉽게 (0) | 2011.12.07 |