使用WPF动态加载内容

时间:2012-05-24 16:26:56

标签: c# wpf datatemplateselector

好的我有一个容器,我已经创建了两个数据模板。基本上,一个模板将显示5个文本框,并且对象数据绑定到它们,另一个模板将显示添加该特定对象的按钮。我将DataTemplateSelector子类化并且它可以工作,但是当我浏览我的记录时,Selector永远不会被再次调用。

那么我如何让容器重新选择它的模板。容器是StackPanel,我已经尝试过UpdateVisuals,InvalidateVisuals,InvalidateArrange和ApplyTemplate。

XAML代码

<DataTemplate x:Key="advisorTemplate">
        <StackPanel Orientation="Vertical" Margin="2,2,2,2" HorizontalAlignment="Stretch" VerticalAlignment="Top">
            <StackPanel Orientation="Horizontal" Margin="2,2,2,2" HorizontalAlignment="Center">
                <extToolkit:WatermarkTextBox Name="txtAcadAdv" Watermark="Acad Adv" Width="125" Margin="2" Text="{Binding Path=Adv.AcadAdv}"/>
                <extToolkit:WatermarkTextBox Name="txtProgAdv" Watermark="Prog Adv" Width="125" Margin="2" Text="{Binding Path=Adv.ProgAdv}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="2,2,2,2" HorizontalAlignment="Center">
                <extToolkit:WatermarkTextBox Name="txtPortAdv" Watermark="Port Adv" Width="125" Margin="2" Text="{Binding Path=Adv.PortAdv}"/>
                <extToolkit:WatermarkTextBox Name="txtEleTws" Watermark="Ele Tws" Width="125" Margin="2" Text="{Binding Path=Adv.EleTws}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="2,2,2,2" HorizontalAlignment="Center">
                <extToolkit:WatermarkTextBox Name="txtMatTws" Watermark="Mat Tws" Width="125" Margin="2" Text="{Binding Path=Adv.MatTws}"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="addAdvisor">
        <Button HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2" Name="btnAddAdvisor" Click="ButtonClick" Content="Add Advisor"/>
    </DataTemplate> 

在组框上更改内容的初始化

grpAdv.ContentTemplateSelector = _advisorSelector;

最后是选择器代码

private readonly StudentWin _win;

    public  AdvisorDataTemplateSelector(StudentWin win)
    {
        _win = win;
    }

    public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
    {
        var sp = item as StackPanel;
        var adv = sp.DataContext as Advisor;


        if (adv == null)
            return _win.FindResource("addAdvisor") as DataTemplate;

        return _win.FindResource("advisorTemplate") as DataTemplate;
    }

以下是我导航代码的片段

case "btnNext":
                    {
                        if(_view.CurrentPosition < _view.Count - 1)
                        {
                            CheckForUnusedReferences(_view.GetItemAt(_view.CurrentPosition) as Student);
                            _view.MoveCurrentToNext();
                            CheckForNullReferences(_view.CurrentPosition);
                            grpAdv.ApplyTemplate();
                        }
                    }

另外两个方法是检查学生之间的关系是否为空,他们将创建它并将其添加到数据上下文中,否则实体框架将不会保存更改。上面的数据模板基本上可以帮助我解决在我尝试创建新学生时没有studentId的问题。

1 个答案:

答案 0 :(得分:2)

我使用以下方法强制重新应用DataTemplateSelector。

从ObservableCollection派生并添加一个使用NotifyCollectionChangedAction.Reset引发NotifyCollectionChangedEventArgs的方法。

public class MyThingCollection : ObservableCollection<MyThing>
{
    public void RaiseResetCollection()
    {
        OnCollectionChanged(new 
            NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }
}

您的视图模型会公开此类型的实例,而ItemsControl会绑定到该类型。

public class MyViewModel : ... (view model base)
{
    public MyThingCollection Items{get; private set;}
}

<ItemsControl
     ItemsSource="{Binding Items}"
     ItemsTemplateSelector="{StaticResource MyTemplateSelector}"
     ...

当您需要重新应用DataTemplateSelector时,请在集合上调用RaiseResetCollection。

我通常使用像这样的DataTemplateSelector

public class MyTemplateSelector : DataTemplateSelector
{
    public DataTemplate Template1 { get; set; }
    public DataTemplate Template2 { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        ... return Template1 or Template2 depending on item
    }
    ...
}

<DataTemplate x:Key="MyTemplate1" DataType="{x:Type MyType1}">
    ...
</DateTemplate>

<DataTemplate x:Key="MyTemplate2" DataType="{x:Type MyType2}">
    ...
</DateTemplate>

<local:MyTemplateSelector 
    x:Key="MyTemplateSelector" 
    Template1="{StaticResource MyTemplate1}"
    Template2="{StaticResource MyTemplate2}"
/>