我正在使用MVVM,每个View都映射到具有约定的ViewModel.IE MyApp.Views.MainWindowView MyApp.ViewModels.MainWindowViewModel
有没有办法删除DataTemplate并在C#中执行?有某种循环?
<DataTemplate DataType="{x:Type vm:MainWindowViewModel}">
<vw:MainWindowView />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud) 正如我所看到的,很多人遇到了这个确切的问题,但我无法理解为什么我的情况不起作用而且它开始让我发疯.
上下文:我有一个DataGrid根据每个单元格的值着色的.因此,我有一个动态样式解析用于每个单元格的实际模板.背景现在相应地起作用.
新问题:当我有一个深色背景时,我希望字体颜色为白色,字体粗细为粗体,以便文本可以正确读取.而且......我无法正确设计它.
我读了一些关于它的Stackoverflow帖子:
这个适合我的问题,但没有提供任何工作解决方案 这一个也很清楚,细节但是...... 这个问题和我几乎一样但是......解决方案不起作用
这是我到目前为止尝试的内容:
<!-- Green template-->
<ControlTemplate x:Key="Green" TargetType="{x:Type tk:DataGridCell}">
<Grid Background="Green">
<ContentPresenter
HorizontalAlignment="Center"
VerticalAlignment="Center">
<ContentPresenter.Resources>
<Style BasedOn="{StaticResource BoldCellStyle}" TargetType="{x:Type TextBlock}" />
</ContentPresenter.Resources>
</ContentPresenter>
</Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
不行.背景为绿色,但文字保持黑色而不是粗体.
BTW,BoldCellStyle尽可能简单:
<Style x:Key="BoldCellStyle" TargetType="{x:Type TextBlock}">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="White" />
</Style>
Run Code Online (Sandbox Code Playgroud)
好的.第二次尝试(这是一个真正的愚蠢但很好......)
<!-- Green template -->
<ControlTemplate x:Key="Green" TargetType="{x:Type tk:DataGridCell}">
<Grid Background="Green">
<ContentPresenter
HorizontalAlignment="Center"
VerticalAlignment="Center">
<ContentPresenter.Resources>
<Style x:Key="BoldCellStyle" TargetType="{x:Type TextBlock}">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="White" />
</Style>
</ContentPresenter.Resources>
</ContentPresenter>
</Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
也不起作用.
然后,我试着玩这个 …
我正在尝试创建一个DataTemplate,用于映射具有相应视图的简单数据类型,如下所示:
<DataTemplate DataType="{x:Type src:Person}">
<TextBox Text="{Binding Name}"/>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我收到一个编译器错误,指示无法识别或访问DataType属性.我在这里错过了什么吗?是否有新的语法来执行此操作或缺少功能?是否存在隐式模板的替代解决方案?
作为参考,这里是使用ax:Key属性(可以工作)使用DataTemplate限定的完整代码:
<UserControl x:Class="Metro_App.MainPage"
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:src="clr-namespace:Metro_App"
mc:Ignorable="d"
d:DesignHeight="768" d:DesignWidth="1366">
<UserControl.Resources>
<DataTemplate x:Key="PersonTemplate">
<TextBlock Text="{Binding Name}" Foreground="White" FontSize="72"/>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="#FF0C0C0C">
<ContentControl Content="{Binding MyPerson}" ContentTemplate="{StaticResource PersonTemplate}"/>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud) 我有一个Silverlight应用程序,显示一个项目列表ListBox.每个项目代表我的应用程序的不同"页面",所以我有一个应用于ItemContainerStyle属性的样式,如下所示:
<Style x:Key="navigationItemContainerStyle" TargetType="ListBoxItem">
<Setter Property="Margin" Value="5,3"/>
<Setter Property="FontSize" Value="16"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Cursor="Hand">
<VisualStateManager.VisualStateGroups>
<!-- code omitted --!>
</VisualStateManager.VisualStateGroups>
<Border x:Name="contentBorder"
Background="{StaticResource navigationHighlightBrush}"
CornerRadius="3"
Opacity="0"/>
<ContentControl x:Name="content"
Margin="10,5"
Content="{Binding}"
Foreground="DarkGray"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
风格很简单.它仅仅是显示Border当ListBoxItem的视觉状态等于'选定’.请注意,内容由a托管,ContentControl因为我希望能够Foreground在项目处于"已选择"状态时更改属性.
这可以很好地工作,如下面的屏幕截图所示:

现在我希望所选项目调用导航,所以我的想法是创建一个DataTemplate将每个项目的内容设置为HyperLinkButton:
<DataTemplate x:Key="navigationListBoxItemTemplate">
<HyperlinkButton Content="{Binding}"
Background="Transparent"/>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
现在这不能作为ItemTemplate主机的内容ContentControl而不是一样,ContentPresenter所以我必须更新ListBoxItem模板才能使用ContentPresenter.
<ContentPresenter x:Name="content" Margin="10,5"/>
Run Code Online (Sandbox Code Playgroud)
我现在得到以下结果:

当我在点击HyperLinkButton …
我在ListView上使用DataTemplate for SelectedItem:
<ControlTemplate x:Key="SelectedTemplate" TargetType="ListViewItem">
<WrapPanel (...) BackGround="Silver">
</WrapPanel>
(...)
<Style TargetType="ListViewItem">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
<Condition Property="Selector.IsSelectionActive" Value="true" />
</MultiTrigger.Conditions>
<Setter Property="Template" Value="{StaticResource SelectedTemplate}" />
</MultiTrigger>
</Style.Triggers>
</Style>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
此外,ItemsSource绑定到IObservableCollection.但是我在myListView中选择项目时遇到了一些问题.我想要达到的是,默认项目模板具有白色背景.选定的背景是银.当我点击项目时,背景会发生变化.但是当我从代码中执行此操作时,listview已选择项目(选中,选中index = 0,selectetitem!= null),但项目从非选定项目获取样式.所以基本上我想用selectedTemplate选择项目.我已经尝试了myListView.SelectedIndex,myLisview.SelectedItem,但实际上并没有工作..有什么想法吗?
谢谢!
我有这样的事情:
<Window>
<Window.Resources>
<DataTemplate DataType="{x:Type local:VM1}">
<!-- View 1 Here -->
</DataTemplate>
<DataTemplate DataType="{x:Type local:VM2}">
<!-- View 2 here -->
</DataTemplate>
<Window.Resources>
<ContentPresenter Content="{Binding}"/>
</Window>
Run Code Online (Sandbox Code Playgroud)
当我绑定不同的视图模型时,这将自动交换视图,这非常方便。
但是,我有一个带有tabcontrol和许多子视图的视图。每个子视图都有几个可视部分,这些可视部分由自定义xml文件配置(复杂的业务案例)。每次创建此视图时,都会解析xml文件,这会导致较小的延迟(1-2秒)。烦人并使UI呆滞足以延迟。
有没有一种方法可以使用DataTemplate模式而无需每次绑定视图模型时都销毁和重新创建视图?如果可能,我宁愿不更改视图模型。
我确信这很简单,但我似乎无法弄清楚如何做到这一点.基本上我有一个来自天蓝色移动服务数据库的客户列表.到目前为止一切正常但我想根据数据为列表框中的每个项目设置项目模板.我有两个模板,一个用于公司,一个用于一个人.我的问题是如何应用每一个.
模板
<DataTemplate x:Key="CompanyItemTemplate">
-------
</DataTemplate>
<DataTemplate x:Key="CustomerItemTemplate">
-------
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
码
CustomerListItems.ItemsSource = customeritems.OrderBy(customer => customer.CustomerName);
foreach (Customers customer in customeritems)
{
if (customer.Company != "")
{
CustomerListItems.ItemTemplate = CompanyItemTemplate;
}
else
{
CustomerListItems.ItemTemplate = CustomerItemTemplate;
}
}
Run Code Online (Sandbox Code Playgroud) 我有:
<DataTemplate DataType="{x:Type svgEdit:UserControlSvgEditModel}">
<svgEdit:UserControlSvgEdit />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我想以编程方式获取类型:svgEdit:UserControlSvgEdit
我做:
// Here the obj Type is the key to the resource, it works but
var key = new System.Windows.DataTemplateKey(obj.GetType());
var dataTemplate = (DataTemplate)DockSite.FindResource(key);
// I don't know how to get the DataTemplate associated type ????
var tc = dataTemplate.Template as TemplateContent;
Run Code Online (Sandbox Code Playgroud)
但我找不到如何检索相关类型?
注意:我需要以编程方式实例化模板内容并将其传递给DockSite(Docking Manager)
我遇到过问题,使用ScrollViewer.
这是示例视图模型:
public class A
{
public string Text { get; set; }
}
public class B
{
public int Number { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
......和DataTemplateSelector:
public class ViewModelTemplateSelector : DataTemplateSelector
{
public DataTemplate ATemplate { get; set; }
public DataTemplate BTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item is A)
return ATemplate;
if (item is B)
return BTemplate;
return base.SelectTemplate(item, container);
}
}
Run Code Online (Sandbox Code Playgroud)
XAML:
<Grid>
<Grid.Resources>
<local:ViewModelTemplateSelector …Run Code Online (Sandbox Code Playgroud) 我正在使用VS 2015中的数据模板绑定开发一个简单的UWP项目.当我尝试指定Datatemplate的类型时,我收到一个错误.
XAML:
<Page x:Name="RootPage"
x:Class="Adaptive_News_Layout.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Adaptive_News_Layout"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" FontSize="22" >
<SplitView x:Name="MySplitView" Grid.Row="1" DisplayMode="CompactOverlay" Background="LightGray" OpenPaneLength="200" >
<SplitView.Pane>
<ListView x:Name="MyListview" ItemsSource="{x:Bind NavigationItems}" >
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:NavItem" >
<StackPanel Orientation="Horizontal">
<RelativePanel>
<Button x:Name="Icon" FontFamily="Segoe MDL2 Assets" Content="{x:Bind ButtonIcon}" Width="50" Height="50"/>
<TextBlock x:Name="Section" Text="{x:Bind SectionTitle}" RelativePanel.RightOf="Icon" />
</RelativePanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
这是班级:
namespace Adaptive_News_Layout
{
public class NavItem
{
public string ButtonIcon { get; set; }
public string SectionTitle { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
错误如下:名称"NavItem"在名称空间"using:Adaptive_News_Layout"中不存在
datatemplate ×10
c# ×7
wpf ×6
datagrid ×1
listboxitem ×1
listview ×1
mvvm ×1
resources ×1
scrollviewer ×1
selected ×1
silverlight ×1
styles ×1
uwp ×1
uwp-xaml ×1
windows-8 ×1
windows-8.1 ×1
winrt-xaml ×1
xaml ×1