- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不明白为什么当我在 WPF 中切换选项卡 TreeView 的扩展时,它会影响所有选项卡 TreeView 的扩展。我希望每个选项卡的 TreeView 彼此独立。这是一个非常简单的 MVVM 设置,有几个类。
这是项目中的文件
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:data="clr-namespace:WpfApplication1"
xmlns:view="clr-namespace:WpfApplication1.View"
WindowStartupLocation="CenterScreen"
Title="MainWindow" Height="350" Width="250">
<Window.DataContext>
<data:ViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--<Button Content="Add" Command="{Binding AddCommand}" Grid.Row="0"></Button>-->
<TabControl x:Name="tabControl1" ItemsSource="{Binding TabItems}" Grid.Row="1" Background="LightBlue">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}" VerticalAlignment="Center"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<view:TabItemView />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
</Window>
<UserControl x:Class="WpfApplication1.View.TabItemView"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Content}" />
<TreeView Grid.Row="1" Background="Transparent">
<TreeViewItem Header="Favorites">
<TreeViewItem Header="USA"></TreeViewItem>
<TreeViewItem Header="Canada"></TreeViewItem>
<TreeViewItem Header="Mexico"></TreeViewItem>
</TreeViewItem>
</TreeView>
</Grid>
</UserControl>
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public class ViewModel
{
private ObservableCollection<TabItem> tabItems;
public ObservableCollection<TabItem> TabItems
{
get { return tabItems ?? (tabItems = new ObservableCollection<TabItem>()); }
}
public ViewModel()
{
TabItems.Add(new TabItem { Header = DateTime.Now.ToString("Tab 1"), Content = DateTime.Now.ToString("F") });
TabItems.Add(new TabItem { Header = DateTime.Now.ToString("Tab 2"), Content = DateTime.Now.ToString("F") });
TabItems.Add(new TabItem { Header = DateTime.Now.ToString("Tab 3"), Content = DateTime.Now.ToString("F") });
}
}
public class TabItem
{
public string Header { get; set; }
public string Content { get; set; }
}
}
最佳答案
TabControl
为每个项目重复使用相同的内容元素。它所做的只是更改绑定(bind)的数据上下文(即您的 View 模型),更新绑定(bind)到 View 模型的模板化元素中的元素。因此,当您从一个选项卡切换到下一个选项卡时, View 的其他状态保持不变。
每次更改选项卡时都可以强制创建新的内容元素;一种方法是将内容元素的模板声明为资源,添加 x:Shared="False"
到资源声明,然后将资源用作 Setter
的值应用于 Style
针对TabItem
类型。
通过 setter 将模板应用到每个 TabItem
是必需的 - 和 TabItem
这里我的意思是 WPF TabItem
,而不是您的 View 模型类,如果我是您,我会更改其名称以避免混淆。使用 x:Shared
如果您只设置 TabControl.ContentTemplate
将无济于事直接一次。
例如:
<TabControl.Resources>
<DataTemplate x:Key="tabItemTemplate" x:Shared="False">
<l:TabItemView />
</DataTemplate>
<s:Style TargetType="TabItem">
<Setter Property="ContentTemplate" Value="{StaticResource ResourceKey=tabItemTemplate}"/>
</s:Style>
</TabControl.Resources>
bool
属性到您的 View 模型以记住每个项目的当前设置并将其绑定(bind)到
IsExpanded
顶级属性
TreeViewItem
:
public class TabItem
{
public string Header { get; set; }
public string Content { get; set; }
public bool IsExpanded { get; set; }
}
<UserControl x:Class="TestSO33125188TreeViewTemplate.TabItemView"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Content}" />
<TreeView Grid.Row="1" Background="Transparent">
<TreeViewItem Header="Favorites" IsExpanded="{Binding IsExpanded, Mode=TwoWay}">
<TreeViewItem Header="USA"></TreeViewItem>
<TreeViewItem Header="Canada"></TreeViewItem>
<TreeViewItem Header="Mexico"></TreeViewItem>
</TreeViewItem>
</TreeView>
</Grid>
</UserControl>
Mode
属性(property)给
TwoWay
, 默认为
OneWay
并且不会以其他方式复制当前的
TreeViewItem.IsExpanded
值返回给 View 模型。
INotifyPropertyChanged
或类似的机制。每当更改当前选项卡时,WPF 都会强制将更新的属性值复制回 View 。但就个人而言,我会继续添加
INotifyPropertyChanged
执行;在 WPF 不会自动检测到您已更新属性值的不同场景中,很容易发现自己重用该技术,从而导致令人沮丧的错误,需要一段时间才能跟踪和修复。
关于c# - 如果 MVVM 模型继承扩展到所有选项卡,为什么 Treeview 会扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33125188/
如何将浮点 10 字节十六进制字符串(Delphi 中的扩展数据类型)转换为 C# 数据类型? 例如:00 00 00 00 00 00 00 80 ff 3f 在 Delphi 1 最佳答案 涉及同
我正在阅读 this page上面写着 If one of the strings has an encoding prefix and the other doesn't, the one that
我有以下代码(作为示例),我想对其进行调整,以使功能区扩展到整个 xrange,如 geom_hline()做。功能区指示哪些值在可接受的范围内。在我的实际应用程序中,有时没有上限或下限,因此 hli
去年,斯科特·格思里 stated “如果您想要对执行的 SQL 进行绝对控制,您实际上可以覆盖 LINQ to SQL 使用的原始 SQL”,但我找不到描述可扩展性方法的文档。 我想修改以下 LIN
我尝试在我的应用中创建一个抽屉导航。我有 3 个当前选项(查看、声明、报告),当然这 3 个 Activity 已扩展到 fragment。在View 中,它有一个next 按钮,该按钮将转到另一个页
很抱歉,如果我的问题被重复,但我无法(或不知道如何谷歌)找到答案。我想使用 rxlifecycle-navi 库来将 rxjava 绑定(bind)到生命周期,所以我需要扩展 NaviActivity
有这个html: aaa 做的时候: $('#hi1').sayHi(); 我想收到一条“嗨”提醒 有人知道这是怎么做到的吗? 最佳答案 了解更多关于jQuery plugin authoring
我有一个 UIImageView 和一个 UITextView。当点击 UITextView 时,我需要 UITextView 在 UIImageView 上展开。我不知道该怎么做。 从这里 为此 最
以下语句: URLClassLoader ucl = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class uclc = ucl.get
以下语句: URLClassLoader ucl = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class uclc = ucl.get
$的意思是什么!在 shell 或 shell 脚本中?我正在尝试理解具有以下内容的脚本。 local@usr> a=1 local@usr> echo $a 1 local@usr> echo $!
我正在 GWT 中设计一个简单的应用程序,并且在客户端有不同的 View 。每个 View 都从 SimplePanel 扩展而来,并将根据需要添加到 RootPanel。 我的第一个 View 是
我尝试在 SQL Azure 选项卡中从 S2 扩展到 S3,但 S3 似乎不可用,尽管它已被引入。另外,我还有大量可用的 DTU,即 1900 左右。是否无法扩展到 S3?我是否需要导出数据库,然后
我有一个带有 JS、html 和 css 的自定义视频播放器。我的问题的关键是我没有预料到将其从一个视频扩展到两个视频,我希望重构它,以便我可以在一个页面上播放多个视频。我尝试将所有内容重写为 for
我当前正在开发的应用程序需要可扩展的实时通信。我们一直在研究并尝试 Firebase 实时数据库和 firestore。看来Firebase实时数据库更加成熟并且经过测试,而firestore仍处于测
当 pod 被 daemonset 控制时,pod 会出现一些错误,状态会是 CrashLoopBackOff ,我想删除这些 Pod 但不想删除 DaemonSet。 所以我想把daemonset缩
我有 Jframe 表单并创建了从中绘制图形的类,为此我需要将该 void 类扩展为 jframe 表单 package grafiktest; import java.awt.Color; impo
在布局和尺寸策略方面,我从来都不是太出色,所以这可能是一个简单的答案,但就是这样。 当我向 QTabWIdget 添加新选项卡并将小部件设置为 QSplitter 时,拆分器将使用给定的全部空间量。但
我在将我的 S4 对象转换回列表时遇到了一些问题。例如,以下嵌套的 S4 类: setClass("nssItem", representation(value = "numeric
在阅读了 DDD 的一些页面后,我了解了存储库模式,然后在应用程序中成功使用了它(我真的很喜欢它);然后我注意到 EntityManager 是 generic-repository-like,但是当
我是一名优秀的程序员,十分优秀!