简体   繁体   English

UWP Xaml,在运行时更改ContentControl的DataTemplate,具体取决于其DataContext的属性值

[英]UWP Xaml, changing ContentControl's DataTemplate at runtime, Depending on the value of a property on its DataContext's

Please I have a ContentControl , which displays the details of a items clicked on a ListView , the Item clicked is set at runtime as the Content control's DataContext , and I am willing to know how to change this ContentControl 's DataTemplate every time the Datacontext is updated, depending on a value of the DataContext a DataTemplateSelector but, the template is set only once and when the DataContext changes, this initial template doesn't update if it is suposed to. 请给我一个ContentControl ,它显示在ListView上单击的项的详细信息,在运行时将单击的项设置为Content控件的DataContext ,并且我想知道每次Datacontext更改此ContentControlDataTemplate的方法。更新,取决于DataContext的值是DataTemplateSelector但是模板仅设置一次,并且当DataContext更改时,此初始模板不会被更新。 So, I decided to proceed by using VisualTriggers , but this seems not to work either, since the DataTemplate does not even show when I use this approach. 因此,我决定继续使用VisualTriggers ,但这似乎也不起作用,因为当我使用这种方法时, DataTemplate甚至都不会显示。 This is my code when I used DataTriggers: 这是我使用DataTriggers时的代码:

<ContentControl x:Name="baseTemplate" DataContext="{Binding Selected}">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup>
                        <VisualState x:Name="Uno">
                            <VisualState.StateTriggers>
                                <dataTriggers:FocusDataTrigger
                                dataTriggers:FocusDataTrigger.TriggerValue="Uno"
                                dataTriggers:FocusDataTrigger.DataValue="{Binding DataContext.Type}"/>
                            </VisualState.StateTriggers>
                            <VisualState.Setters>
                                <Setter Target="baseTemplate.Template" Value="{StaticResource UnoTemplate}"/>
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Duo">
                            <VisualState.StateTriggers>
                                <dataTriggers:FocusDataTrigger
                                dataTriggers:FocusDataTrigger.TriggerValue="Duo"
                                dataTriggers:FocusDataTrigger.DataValue="{Binding  DataContext.Type}"/>
                            </VisualState.StateTriggers>
                            <VisualState.Setters>
                                <Setter Target="baseTemplate.Template" Value="{StaticResource detailTemplateSelector}"/>
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </ContentControl>

I wish to make the DataTemplate change everytime the value set to the DataContext has a property Type of value "Uno" or "Duo" Please help!, I have tried as much as I could, but I can't find a solution for this. 我希望everytime设置为DataContext的值具有属性Type"Uno""Duo"的值时都可以更改DataTemplate请帮忙!,我已经尽力了,但是我找不到解决方案。

I use a custom DataTrigger 我使用自定义DataTrigger

public class FocusDataTrigger : StateTriggerBase
{
    #region DataValue
    public static readonly DependencyProperty DataValueProperty =
        DependencyProperty.RegisterAttached("DataValue", typeof(object),
            typeof(FocusDataTrigger), new PropertyMetadata(null, DataValueChanged));

    public static object GetDataValue(DependencyObject obj)
    {
        return obj.GetValue(DataValueProperty);
    }
    public static void SetDataValue(DependencyObject obj, object value)
    {
        obj.SetValue(DataValueProperty, value);
    }
    #endregion

    #region TriggerValue

    public static readonly DependencyProperty TriggerValueProperty =
        DependencyProperty.RegisterAttached("TriggerValue", typeof(object),
            typeof(FocusDataTrigger), new PropertyMetadata(false, TriggerValueChanged));


    public static object GetTriggerValue(DependencyObject obj)
    {
        return obj.GetValue(TriggerValueProperty);
    }
    public static void SetTriggerValue(DependencyObject obj, object value)
    {
        obj.SetValue(TriggerValueProperty, value);
    }

    #endregion

    private static void TriggerStateCheck(DependencyObject target, object datavalue, object triggerValue)
    {
        FocusDataTrigger trigger = target as FocusDataTrigger;
        if (trigger == null) return;
        trigger.SetActive(triggerValue == datavalue);
    }
    private static void TriggerValueChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        object datavalue = target.GetValue(FocusDataTrigger.DataValueProperty);
        TriggerStateCheck(target, datavalue, e.NewValue);
    }
    private static void DataValueChanged(DependencyObject target,
        DependencyPropertyChangedEventArgs e)
    {
        object triggerValue = target.GetValue(FocusDataTrigger.TriggerValueProperty);
        TriggerStateCheck(target, e.NewValue, triggerValue);
    }
}

It seems you are going to a wrong direction. 看来您走错了方向。

Your template will change based on a property changed. 您的模板将根据更改的属性而更改。

I'm not so sure what you mean here. 我不太确定你在这里的意思。 DataContextChanged means you are switching your datacontext. DataContextChanged意味着您正在切换数据上下文。 But it seems here you just want to change things based on a property value, am I right on this? 但是似乎您只是想根据属性值更改某些东西,我是否正确?

Try do something with the following steps: 请尝试执行以下步骤:

  1. Create a DataTemplateSelector: 创建一个DataTemplateSelector:

     public class CCDataTemplateSelector: DataTemplateSelector { public DataTemplate FirstTemplate { get; set; } public DataTemplate SecondTemplate { get; set; } protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) { var model = item as DemoModel; if (model == null) return null; if (model.ID >= 2) return FirstTemplate; else return SecondTemplate; } } 
  2. Then in your XAML you can write something like this: 然后,在您的XAML中,您可以编写如下内容:

      <StackPanel.Resources> <DataTemplate x:Key="FirstTemplate"> xxxxx </DataTemplate> <DataTemplate x:Key="SecondTemplate"> xxxxx </DataTemplate> <local:CCDataTemplateSelector FirstTemplate="{StaticResource FirstTemplate}" SecondTemplate="{StaticResource SecondTemplate}" x:Key="DataTemplateSelector" /> <Style TargetType="ContentControl"> <Setter Property="ContentTemplateSelector" Value="{StaticResource DataTemplateSelector}" /> </Style> </StackPanel.Resources> 

In this way the template will different based on the value you set in your model. 这样,模板将根据您在模型中设置的值而有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM