English 中文(简体)
用 WPF 动态装入内容
原标题:Dynamically Load Content with WPF

Ok 我有一个容器, 我为它创建了两个数据模板。 基本上一个模板将显示5个文本框, 并显示与它们连接的数据, 而另一个模板将显示添加该特定对象的按钮。 我分级的数据模板选择器和它有效, 但当我浏览记录时, “ 选择器” 将永远不再被调用 。

那么我该如何让容器重新选择其模板 。 容器是一个堆积板, 我已经尝试过“ 更新视觉”、“ 无效视觉”、“ 无效结构” 和“ 应用模板 ” 。

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();
                        }
                    }

另外两种方法是检查学生是否不存在关系,然后创建关系并将其添加到我的数据背景中,否则实体框架将无法保存这些变化。 以上数据模板将基本上帮助我解决一个问题,即当我试图创建新学生时,没有学生身份标识。

最佳回答

我使用了以下方法强制重新应用数据板选择器。

从可观察集合中衍生出来, 并添加一种方法, 藉由通知集合变化的平衡点, 来提高提醒变化的集合变化的平衡点。 重置 。

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

您的视图模型暴露了这种类型的实例, 您的物品控制系统与此相关 。

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

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

当您需要您的数据模板选择器在收藏中重新应用时, 需要重新调用调用 Reserve Reset Calltion 。

我通常使用这样的数据模板选择器

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}"
/>
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签