我有一个IList绑定到a的viewmodels TabControl.这IList不会改变整个生命周期TabControl.
<TabControl ItemsSource="{Binding Tabs}" SelectedIndex="0" >
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="Content" Value="{Binding}" />
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
Run Code Online (Sandbox Code Playgroud)
每个viewmodel都有一个DataTemplate在a中指定的ResourceDictionary.
<DataTemplate TargetType={x:Type vm:MyViewModel}>
<v:MyView/>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
DataTemplate中指定的每个视图都是资源密集型的,足以创建我只想创建每个视图一次,但是当我切换选项卡时,会调用相关视图的构造函数.从我所读到的,这是预期的行为TabControl,但我不清楚调用构造函数的机制是什么.
我已经看过一个使用UserControls 的类似问题,但是那里提供的解决方案需要我绑定到不受欢迎的视图.
我只是想了解以下场景.我在哪里实施一个<TabControl>绑定ObservableCollection<TabViewModel>
没有数据模板
当我没有任何DataTemplates时,文本WpfApplication1.TabViewModel显示在选项卡标题和内容中.好的,我理解这一部分.
只是 ItemTemplate
当我有
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding TabTitle}" />
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
然后填充了我的标题页眉.标签内容仍然是WpfApplication1.TabViewModel.
只是 DataTemplate
当我在我的下面有我的 <Window.Resources>
<DataTemplate DataType="{x:Type local:TabViewModel}">
<TextBox Text="{Binding Text}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
该模板填充了标题页眉.
都
如果我ItemTemplate同时DataTemplate填写选项卡内容,则填充选项卡标题.为何所有这些差异.ItemTemplate&DataTemplate填满的标签标头如果对方是不存在.如果两者都存在,ItemTemplate则在DataTemplate填充内容时填充标题.
虽然我有事情可行,但我很困惑.不<TabControl.HeaderTemplate>应该是填充标题和<TabControl.ItemTemplate>填写内容的东西吗?
有没有人知道是否以及如何ListBox根据属性的值禁用数据绑定中的项目?
我想要一个DataTrigger,当某个属性是false,禁用这个项目(使其无法选择)而不影响其中的其他项目ListBox.
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Name="textBlock" Text="{Binding Description}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsEnabled}" Value="False">
??
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习WPF的一些内容,我对它的灵活性感到非常惊讶.
但是,我遇到了Styles和DataTemplates 的问题,这有点令人困惑.我已经定义了以下测试页,与周围风格等有点玩,发现Style在定义小号<Page.Resources>的Border,并TextBlock没有在应用DataTemplate,但Style对于ProgressBar以完全相同的方式定义应用.
源代码(我只使用Kaxaml和XamlPadX来查看结果)
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="Background" Value="SkyBlue"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="CornerRadius" Value="5"/>
</Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontWeight" Value="Bold"/>
</Style>
<Style TargetType="{x:Type ProgressBar}">
<Setter Property="Height" Value="10"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Foreground" Value="Red"/>
</Style>
<XmlDataProvider x:Key="TestData" XPath="/TestData">
<x:XData>
<TestData xmlns="">
<TestElement>
<Name>Item 1</Name>
<Value>25</Value>
</TestElement>
<TestElement>
<Name>Item 2</Name>
<Value>50</Value>
</TestElement>
</TestData>
</x:XData> …Run Code Online (Sandbox Code Playgroud) 我有一个ListBox,它的ItemTemplate绑定到DataTemplate.我的问题是我无法将模板中的元素拉伸到ListBox的整个宽度.
<ListBox x:Name="listPeople" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
Margin="0,0,0,0" Background="{x:Null}" SelectionMode="Extended" Grid.Row="1"
ItemTemplate="{StaticResource PersonViewModel.BrowserDataTemplate}"
ItemsSource="{Binding Mode=OneWay, Path=SearchResults}" >
</ListBox>
<DataTemplate x:Key="PersonViewModel.BrowserDataTemplate">
<ListBoxItem HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5,5,5,5">
<Border Opacity=".1" x:Name="itemBorder" Background="#FF000000"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
CornerRadius="5,5,5,5" MinWidth="100" Height="50"/>
</Grid>
</ListBoxItem>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
如您所见,我在网格中添加了一个边框以指示模板的宽度.我的目标是看到此边框扩展到列表框的整个宽度.目前它的宽度由其内容或MinWidth决定,这是目前保持可见的唯一内容.
在我的项目中,我有一个绑定到ObservableCollection的WPF Listbox.每次我向Collection添加一个新项目时,相同的项目会自动添加到Listbox中.要显示列表框中的项目,我使用XAML Datatemplate.
我想要做的是在项目添加到Collection/Listbox时为项目设置动画.可以这样做吗?作为datatemplate中的动画可能吗?我想我需要一个触发器以某种方式启动这个动画,但是当添加一个新项目/ datatemplate时会触发什么触发器?
我正在编写一个复合松散耦合的MVVM WPF应用程序,父虚拟机中的子虚拟机是接口而不是类实例,例如
public IChildViewModel { get; set; }
Run Code Online (Sandbox Code Playgroud)
现在如何使用DataTemplate呈现此属性?喜欢:
<DataTemplate DataType="{x:Type contracts:IChildViewModel}">
Run Code Online (Sandbox Code Playgroud)
我理解由于接口的性质(多重继承等),WPF不允许这种直接绑定.但是由于接口应该在松散耦合的应用程序中广泛使用,是否有任何解决方法将DataTemplate绑定到接口?谢谢.
当我尝试指定多个DataTemplates供ContentControl使用以便使用正确的(基于Type)时,我最终得到的内容只是Content的ToString()值.
<ContentControl DataContext="{Binding MyTreeRootViewModels}" Content="{Binding /, Path=CurrentlySelectedTreeViewModel}">
<ContentControl.Resources>
<DataTemplate DataType="x:Type vm:TypeAViewModel">
<StackPanel>
<local:TypeAUserControl />
</StackPanel>
</DataTemplate>
<DataTemplate DataType="x:Type vm:TypeBViewModel">
<StackPanel>
<local:TypeBUserControl />
</StackPanel>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,当CurrentSelectedTreeViewModel返回TypeAViewModel的树节点时,我会看到"MyApp.ViewModel.TypeAViewModel".我希望看到我的TypeAViewModelUserControl.
我尝试在我的一个DataTemplates中放置一个<TextBlock Text ="TESTING"/>元素,只是为了查看问题是否与我的用户控件有关.结果相同.
我有什么想法我做错了吗?
(顺便说一下,CurrentlySelectedTreeViewModel是一个返回我的树视图中当前所选节点的属性.它似乎工作正常 - 当我在树中选择节点时,节点的正确类型名称显示为ContentControl).
有没有简单的方法绑定到DataTemplate中的ToString()方法?我希望TextBlock的Text属性默认使用ToString()作为Text属性,但这不会发生.所以任何简单的方法:
<DataTemplate x:Key="myTemplate">
<TextBlock Text="{Binding ToString()}"/>
<DataTemplate>
Run Code Online (Sandbox Code Playgroud) 我有以下简化示例:
<Window x:Class="TemplateBinding.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>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="pack://application:,,,/TemplateBinding;component/PersonTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ContentControl ContentTemplate="{StaticResource PersonTemplate}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
附:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="PersonTemplate">
<Border Width="100" Height="100" Background="RosyBrown">
<TextBlock Text="{Binding Path=FirstName}" VerticalAlignment="Center" TextAlignment="Center"/>
</Border>
</DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
作为我的DataTemplate在一个单独的ResourceDictionary文件中.
我在我的MainWindow的Construcor中设置了我的DataContext,并通过显示如下的第一个名称来验证它:<ContentControl Grid.Row="1" Content="{Binding FirstName}"/>.
在另一个场景中,我使用DataTemplate与我在DataTemplate中以ListBox完全相同的方式进行绑定,它只是起作用.
我知道DataTemplate除了绑定之外正在工作,因为它正确显示了大小和背景颜色.
我究竟做错了什么?我的DataTemplate中的绑定如何看?
data-binding wpf datatemplate resourcedictionary contentcontrol
datatemplate ×10
wpf ×9
xaml ×5
c# ×2
data-binding ×2
listbox ×2
tabcontrol ×2
animation ×1
bind ×1
interface ×1
mvvm ×1
silverlight ×1
styles ×1
types ×1