如何在Windows Phone中触发datatemplate选择器? [英] How to trigger datatemplate selector in Windows Phone?

查看:124
本文介绍了如何在Windows Phone中触发datatemplate选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个属性,并根据其状态(例如A和B)显示动画或图像的用户控件.

现在,如果属性发生更改,我想再次触发 数据模板选择器.在搜索时,我发现在WPF中我可以使用DataTemplate.Trigger,但它是在WP中不可用.

所以,我的问题是

  • 它们是触发数据模板选择器的一种方法,因此当属性从状态A更改为B时,将选择适当的用户控件.如果是,那么请举例说明如何实现.

此外,由于只有两种状态,如果我可以使用转换器折叠可见性.对于基本的情况,我将需要编写两个转换器.(我能以某种方式仅使用一个转换器吗?) 这是确切的情况.

如果状态== A:

选择userControl_A

else:选择userControl_B

  • 处于折叠状态时,动画用户控件会影响性能吗?

编辑-刚刚意识到,我可以使用参数对象只编写一个转换器.

解决方案

您可以实现如 public override DataTemplate SelectTemplate(object item, DependencyObject container) { City itemAux = item as City; // Subscribe to the PropertyChanged event itemAux.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(itemAux_PropertyChanged); return GetTemplate(itemAux, container); } private DataTemplate GetTemplate(City itemAux, DependencyObject container) { if (itemAux != null) { if (itemAux.Country == "Brazil") return BrazilTemplate; if (itemAux.Country == "USA") return UsaTemplate; if (itemAux.Country == "England") return EnglandTemplate; } return base.SelectTemplate(itemAux, container); } void itemAux_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { // A property has changed, we need to reevaluate the template this.ContentTemplate = GetTemplate(sender as City, this); }

I have a property and depending upon it's state (say A and B) I either show a usercontrol of animation or a image.

Now, if the property changes, I want to trigger the datatemplate selector again. On searching, I found that in WPF I could have used DataTemplate.Trigger but it's not available in WP.

So, my question is

  • Is their a way to trigger datatemplate selector so when property changes from state A to B, then appropriate usercontrol gets selected. If yes, then please give some example how to implement it.

Also, as there are only two states, if think I can use Converter to collapse the visibility. For basic if else situation, I will need to write two converters.( Can I somehow do it using one converter only?) Here is the exact situation.

If state == A :

select userControl_A

else : select userControl_B

Also,

  • Will the animation usercontrol will effect the performance when it's in Collapsed state?

EDIT- Just realized, I can use parameter object to write just one converter.

解决方案

You could implement a DataTemplateSelector like described here.
I use it and it works pretty well.

EDIT:
If you need to update the DataTemplate when the property changes, you should subscribe to the PropertyChanged event of the data object in the TemplateSelector and execute the SelectTemplate method again.

Here is the code sample:

public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
    City itemAux = item as City;

    // Subscribe to the PropertyChanged event
    itemAux.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(itemAux_PropertyChanged);

    return GetTemplate(itemAux, container);
}

private DataTemplate GetTemplate(City itemAux, DependencyObject container)
{
    if (itemAux != null)
    {
        if (itemAux.Country == "Brazil")
            return BrazilTemplate;
        if (itemAux.Country == "USA")
            return UsaTemplate;
        if (itemAux.Country == "England")
            return EnglandTemplate;
    }

    return base.SelectTemplate(itemAux, container);
}

void itemAux_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    // A property has changed, we need to reevaluate the template
    this.ContentTemplate = GetTemplate(sender as City, this);
}

这篇关于如何在Windows Phone中触发datatemplate选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆