gpt4 book ai didi

wpf - 如何防止 WPF 的 ContentControl 重用 DataTemplates?

转载 作者:行者123 更新时间:2023-12-04 17:35:19 27 4
gpt4 key购买 nike

我偶然发现了著名的 TabControl虚拟性问题。我想更换TabControl带有样式列表(用于显示选项卡)和 ContentControl (显示选项卡的内容)。但是,好像ContentControl具有相同的重用行为 DataTemplates如果两个内容共享它们的类型。

有没有办法强制ContentControl实例化单独的 DataTemplates对于所有显示的项目?

编辑:示例

假设我们有以下 UserControl 作为文档 View 模型的 DataTemplate:

文档控件.xaml:

<UserControl x:Class="ControlTemplateProblem.DocumentControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ControlTemplateProblem"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Loaded="UserControl_Loaded"
Unloaded="UserControl_Unloaded"
x:Name="Main">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Index}" />
<TextBlock Text="{Binding ElementName=Main, Path=StoredIndex}" />
</StackPanel>
</UserControl>

DocumentControl.xaml.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ControlTemplateProblem
{
/// <summary>
/// Interaction logic for DocumentControl.xaml
/// </summary>
public partial class DocumentControl : UserControl, INotifyPropertyChanged
{
private bool initialized = false;
private string storedIndex;

public DocumentControl()
{
InitializeComponent();
}

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
StoredIndex = ((ItemViewModel)DataContext).Index;
}

private void UserControl_Unloaded(object sender, RoutedEventArgs e)
{
StoredIndex = "(detached)";
}

public string StoredIndex
{
get => storedIndex;
set
{
storedIndex = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(StoredIndex)));
}
}

public event PropertyChangedEventHandler PropertyChanged;
}
}

主窗口的 View 模型如下所示:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ControlTemplateProblem
{
public class MainViewModel : INotifyPropertyChanged
{
private ItemViewModel selectedItem;

public MainViewModel()
{
selectedItem = Items.First();
}

public ObservableCollection<ItemViewModel> Items { get; } = new ObservableCollection<ItemViewModel> {
new ItemViewModel(),
new ItemViewModel(),
new ItemViewModel()
};

public ItemViewModel SelectedItem
{
get => selectedItem;
set
{
selectedItem = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedItem)));
}
}

public event PropertyChangedEventHandler PropertyChanged;
}
}

主窗口.xaml
<Window x:Class="ControlTemplateProblem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ControlTemplateProblem"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel Orientation="Vertical">
<ListBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />
<ContentControl Content="{Binding SelectedItem}">
<ContentControl.ContentTemplate>
<DataTemplate DataType="{x:Type local:ItemViewModel}">
<local:DocumentControl />
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</StackPanel>
</Window>

我希望文档显示两个相等的索引 - 一个直接来自文档的 View 模型,第二个由控件从文档的 View 模型中提取(这是在附加 View 模型时对控件执行一些初始化的模拟)。启动应用程序并选择不同的项目后,将始终产生 (0) 并且 0 是因为控件仅初始化一次并且 ContentControl 重用了 DataTemplate。

我问的问题的另一个问题是:如何在 DataContext 设置之后和 DataContext 即将更改之前可靠地调用代码?然后我将能够(再次)可靠地初始化和取消初始化特定 View 模型的视觉效果。

最佳答案

我遇到了类似的问题,发现通过将 DataTemplate 的 x:Shared 属性设置为 false,将为每个 View 模型创建一个新的 View 实例。根据documentation for x:Shared ,它只能应用于 ResourceDictionary,但是,因此您可能需要将 View 代码更改为类似以下内容:

<Window x:Class="ControlTemplateProblem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ControlTemplateProblem"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<DataTemplate x:Shared="False" DataType="{x:Type local:ItemViewModel}">
<local:DocumentControl />
</DataTemplate>
</Window.Resources>
<StackPanel Orientation="Vertical">
<ListBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />
<ContentControl Content="{Binding SelectedItem}"/>
</StackPanel>
</Window>

关于wpf - 如何防止 WPF 的 ContentControl 重用 DataTemplates?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56929899/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com