Windows Phone 72011. 12. 22. 10:58

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을 넣어준다.

그래서 선택할수 있다.

참조 : http://windowsphonegeek.com/articles/Implementing-Windows-Phone-7-DataTemplateSelector-and-CustomDataTemplateSelector

소스 :

Posted by 동동(이재동)