我有一组ViewModel,我绑定到TabControl的ItemsSource属性.让我们调用那些ViewModels AViewModel,BViewModel和CViewModel.其中每个都需要有不同的ItemTemplate(对于标题;因为它们每个都需要显示不同的图标)和不同的ContentTemplate(因为它们具有非常不同的交互模型).
我想要的是这样的:
在某处的Resource.xaml文件中定义:
<DataTemplate x:Key="ItemTemplate" DataType="{x:Type AViewModel}">
...
</DataTemplate>
<DataTemplate x:Key="ItemTemplate" DataType="{x:Type BViewModel}">
...
</DataTemplate>
<DataTemplate x:Key="ItemTemplate" DataType="{x:Type CViewModel}">
...
</DataTemplate>
<DataTemplate x:Key="ContentTemplate" DataType="{x:Type AViewModel}">
...
</DataTemplate>
<DataTemplate x:Key="ContentTemplate" DataType="{x:Type BViewModel}">
...
</DataTemplate>
<DataTemplate x:Key="ContentTemplate" DataType="{x:Type CViewModel}">
...
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
单独定义:
<TabControl ItemTemplate="[ Some way to select "ItemTemplate" based on the type ]"
ContentTemplate="[ Some way to select "ContentTemplate" based on the type ]"/>
Run Code Online (Sandbox Code Playgroud)
现在,我真实地知道,每次我使用相同的键定义DataTemplate时,系统就会抱怨.但是,有什么我可以做的类似于这将允许我将DataTemplate放入基于名称和数据类型的TabControl?
我有我使用内部的用户控件DataTemplate,这UserControl包含了TextBox其绑定与价值属性(声明为DependencyProperty我的)UserControl.在数据模板中,我将此Value属性与我的实际属性绑定为Name(也是a DependencyProperty).它工作正常,我在加载时为Name属性赋值,我TextBox显示它,我更改了值TextBox,它也更新了我的Name属性.现在问题出现在我PropertyChangedEventHandler在依赖项属性中添加一个,我检查它是否有效的值,如果有效则不执行任何操作,如果无效则分配旧值.在值无效的情况下,当我给它分配旧的价值,性能更新,但它不是在我的显示更新值TextBox的UserControl.谁能告诉我为什么?
我的XAMLUserControl:
<UserControl x:Name="usercontrol">
<StackPanel>
<TextBlock ......./>
<TextBox Binding={Binding ElementName=usercontrol, Path=Value, Mode=TwoWay}/>
</StackPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
在代码后面,Value是一个DependencyProperty
我在我的使用中DataTemplate:
<HierarchicalDataTemplate DataType="{x:Type myLib:myClass}" ItemsSource="{Binding Children}">
<Expander Header="{Binding}">
<WrapPanel>
<edproperty:TextPropertyEditor Caption="Name" Value="{Binding Name, Mode=TwoWay}"/>
<edproperty:TextPropertyEditor .................../>
<edproperty:TextPropertyEditor .................../>
<edproperty:TextPropertyEditor ...................../>
</WrapPanel>
</Expander>
</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)
我正在使用的这个模板TreeView …
我有一个WPF窗口,其视图模型设置为其DataContext,并且具有一个带有DataTemplate的ListBox,其ItemsSource绑定到视图模型,如下例所示:
查看型号:
using System.Collections.Generic;
namespace Example
{
class Member
{
public string Name { get; set; }
public int Age { get; set; }
}
class Team
{
private List<Member> members = new List<Member>();
public string TeamName { get; set; }
public List<Member> Members { get { return members; } }
}
}
Run Code Online (Sandbox Code Playgroud)
MainWindow.xaml:
<Window x:Class="Example.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:Example"
Title="Example" Height="300" Width="300" Name="Main">
<Window.DataContext>
<l:Team TeamName="The best team">
<l:Team.Members>
<l:Member Name="John Doe" Age="23"/>
<l:Member Name="Jane Smith" Age="20"/>
<l:Member Name="Max Steel" …Run Code Online (Sandbox Code Playgroud) 我已经添加了DataTemplate一个ListBox类来将我的集合绑定到:
<ListBox x:Name="lstEmails" Height="259" Margin="12,0,12,41" Width="276"
SelectionChanged="lstEmails_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Visibility="Hidden" Content="{Binding ID}"></Label>
<TextBox Width="200" Text="{Binding EmailAddress}"></TextBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
这完全符合我的要求.虽然当我点击时TextBox,ListBox不会自动设置关联ListItem的Selected.我可以在代码中执行此操作,但我更愿意将其用作组件(当时没有意外).
关于如何实现这一点的任何想法?
这似乎不起作用,它不会让我点击任何东西.我错过了什么.这是我的新XAML.
<UserControl.Resources>
<!--<TextBox x:Key="TB" x:Name="TextBoxInsideListBoxItemTemplate">
<TextBox.Style>-->
<Style TargetType="{x:Type TextBox}">
<Setter Property="IsHitTestVisible" Value="False" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBoxItem}, AncestorLevel=1}}"
Value="True">
<Setter Property="IsHitTestVisible" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
<!--</TextBox.Style>
</TextBox>-->
</UserControl.Resources>
<Grid>
<ListBox x:Name="lstEmails" Height="259" Margin="12,0,12,41" Width="276" SelectionChanged="lstEmails_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal"> …Run Code Online (Sandbox Code Playgroud) 想象一下,在一个数据模板中,我有一个textBox和另一个数据模板,我有两个文本框.
根据这个,在视图中有一个复选框,并显示每个模板..这可能吗?
对不起,如果我的问题是如此怀疑,我已经调查了但是我没有发现.
我这样做了,我知道这没用,但仅用于测试.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type ContentControl}" x:Key="T1">
<StackPanel>
<TextBox Height="20" />
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type ContentControl}" x:Key="T2">
<StackPanel>
<TextBox Height="20" />
<TextBox Height="20" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ContentControl Template="{StaticResource T1}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud) 假设以下XAML定义窗口:
<Window x:Class="LayoutTests.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LayoutTests"
Title="Window1">
<Window.Resources>
<XmlDataProvider x:Key="XmlData" IsInitialLoadEnabled="True">
<x:XData>
<Items xmlns="">
<Item text="Item 1" type="A" />
<Item text="Item 2" type="B" />
<Item text="Item 3" type="A" />
</Items>
</x:XData>
</XmlDataProvider>
<DataTemplate x:Key="TypeATemplate">
<TextBlock Text="{Binding XPath=./@text}" Foreground="Red"/>
</DataTemplate>
<DataTemplate x:Key="TypeBTemplate">
<TextBlock Text="{Binding XPath=./@text}" Foreground="Green"/>
</DataTemplate>
<DataTemplate x:Key="HeaderTemplate">
<TextBlock Text="A Header"/>
</DataTemplate>
<local:TypeSelector x:Key="TypeSelector" TypeATemplate="{StaticResource TypeATemplate}" TypeBTemplate="{StaticResource TypeBTemplate}"/>
</Window.Resources>
<Grid>
<ListView ItemsSource="{Binding Source={StaticResource XmlData}, XPath='/Items/Item'}" ItemTemplateSelector="{StaticResource TypeSelector}">
<!--<ListView.View>
<GridView>
<GridViewColumn Width="Auto" HeaderTemplate="{StaticResource HeaderTemplate}"/>
</GridView>
</ListView.View>-->
</ListView>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
并DataTemplateSelector …
XAML中的DataTemplate可以与嵌套类相关联吗?
我正在研究MVVM应用程序,我遇到了数据模板问题.我有一个视图模型,为项目控件提供其他视图模型的集合.这些项是在外部视图模型中定义为嵌套类的层次结构的一部分.到目前为止,我还无法在XAML中创建一个映射来引用内部嵌套类.
这是类层次结构(为简洁起见而简化):
public class MainViewModel
{
public class A
{
}
public class B : A
{
}
public class C : A
{
}
public ObservableCollection<A> Items
{
get;
set;
}
}
Run Code Online (Sandbox Code Playgroud)
在XAML中,我正在尝试将DataTemplate映射到类型B和C,但我无法完全限定嵌套类名.
<ItemsControl ItemsSource="{Binding Path=Items}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type ns:BracingViewModel.B}">
<Grid>
....
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type ns:BracingViewModel.C}">
<Grid>
....
</Grid>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
问题:对嵌套类的引用在XAML中显示为构建错误.我得到以下内容:
Error 5 Cannot find the type 'ns:B'. Note that type names are case sensitive. Line...
Error 5 Cannot find the type 'ns:C'. Note that …Run Code Online (Sandbox Code Playgroud) 我已经在网上搜索了很多网站,但没有找到任何解决方案.声明是,UserControl和CustomControl之间没有性能差异.
但我有以下测试类X,UserControl,CustomControl和MainWindow:
public class X : INotifyPropertyChanged
{
private string _title;
public string Title
{
get
{
return _title;
}
set
{
if (value == _title)
{
return;
}
_title = value;
OnPropertyChanged("Title");
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Run Code Online (Sandbox Code Playgroud)
用户控件:
<UserControl x:Class="controlperformance.DisplayView"
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">
<Grid Name="root" Background="LightGray">
<TextBlock Text="{Binding Title}" />
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
CustomControl:
public class …Run Code Online (Sandbox Code Playgroud) 有人可以解释一下我在这里使用的代码是如何工作的吗?
<Window.Resources>
<DataTemplate DataType="{x:Type VM:PBRKEntryViewModel}">
<V:Overview />
</DataTemplate>
<DataTemplate DataType="{x:Type VM:LoginViewModel}">
<V:LoginView />
</DataTemplate>
</Window.Resources>
<Grid>
<ContentPresenter Content="{Binding CurrentView}"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我目前在细节方面的问题是:
我已经找到了这个描述http://msdn.microsoft.com/en-us/library/System.Windows.Controls.ContentPresenter(v=vs.110).aspx但是备注部分没有回答这个问题.(或者我看不到他们......)
再次,为了清楚起见,一切都很完美,但我不明白为什么,所以这只是一个理解选择模板和绑定的问题.
我正在尝试DataTemplate为我设计ItemsControl,我需要一些模拟数据来填充模板.我阅读使用d:DataContext已足够,所以我不必创建一个模拟类.我怎样才能做到这一点?
datatemplate ×10
wpf ×10
xaml ×3
c# ×2
mvvm ×2
.net ×1
data-binding ×1
listbox ×1
listboxitem ×1
listview ×1
nested-class ×1
performance ×1
selection ×1
tabcontrol ×1
templates ×1
textbox ×1