是否可以在Silverlight DataTemplate中绑定事件?如果是这样,最好的方法是什么?
例如,假设您已经创建了一个包含Button的DataTemplate,如下所示:
<UserControl.Resources>
<DataTemplate x:Key="MyDataTemplate" >
<Grid>
<Button Content="{Binding ButtonText}" Margin="4" />
</Grid>
</DataTemplate>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
然后,将它应用于ListBox ItemTemplate,如下所示:
<Grid x:Name="LayoutRoot" Background="White">
<ListBox x:Name="lbListBox" ItemTemplate="{StaticResource MyDataTemplate}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
如果将ListBox的ItemSource设置为该类的对象列表:
public class MyDataClass
{
public string ButtonText{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
那么如何从列表中的DataTemplate中的每个按钮中捕获按钮?你可以使用绑定将Click事件绑定到"MyButtonClass"中的方法,如下所示:
<UserControl.Resources>
<DataTemplate x:Key="MyDataTemplate" >
<Grid>
<Button Click="{Binding OnItemButtonClick}" Content="{Binding ButtonText}" Margin="4" />
</Grid>
</DataTemplate>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
这会有用吗?如果是这样,我应该在"MyDataClass"中加入什么来捕捉事件?
谢谢,杰夫
我有一个DataTemplate我想重用.我要分解的部分是绑定,因为它是唯一改变的东西.我DataTemplate看起来大致像这样.(实际上还有更多的东西,但我已经把外来的东西拿走了.)
<DataTemplate>
<TextBox Text="{Binding Name}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
如何DataTemplate简单地改变我绑定的属性?(请注意,如果它只是一个简单TextBox,我不会担心它,但DataTemplate实际上包含一个StackPane带有许多其他元素的l.我想将它集中在一个地方,因此DataTemplate.)
我想过两种解决这个问题的方法.
DataTemplate.建议?
我有一个DataTemplate,它将是一个模板化的ListBoxItem,这个DataTemplate中有一个ComboBox,当它有焦点时我想要这个模板所代表的ListBoxItem被选中,这看起来对我来说.但遗憾的是它不起作用=(
所以这里真正的问题是在DataTemplate中是否可以ListBoxItem.IsSelected通过DataTemplate.Trigger?获取或设置属性的值?
<DataTemplate x:Key="myDataTemplate"
DataType="{x:Type local:myTemplateItem}">
<Grid x:Name="_LayoutRoot">
<ComboBox x:Name="testComboBox" />
</Grid>
<DataTemplate.Triggers>
<Trigger Property="IsFocused" value="true" SourceName="testComboBox">
<Setter Property="ListBoxItem.IsSelected" Value="true" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
<ListBox ItemTemplate="{StaticResource myDataTemplate}" />
Run Code Online (Sandbox Code Playgroud) 'ContentTemplate'是一个DataTemplate,它显示一个具有成员'FooList'(ObservableCollection)的对象.
<DataTemplate x:Key="ContentTemplate">
<ListBox ItemsSource="{Binding Path=FOO}">
...
</ListBox>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我需要能够使用CollectionViewSource过滤该FooList.这通常是直截了当的,但我似乎无法使绑定在DataTemplate中工作.我试图这样做:
<DataTemplate x:Key="ContentTemplate">
<DataTemplate.Resources>
<CollectionViewSource x:Key="CVS" Source="{Binding Path=FooList}" Filter="FooFilter"/>
<DataTemplate.Resources>
<ListBox ItemsSource="{Binding Source={StaticResource CVS}}">
Run Code Online (Sandbox Code Playgroud)
我从中得到的错误是:
System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement.BindingExpression:路径= FooList; 的DataItem = NULL; target元素是'CollectionViewSource'(HashCode = 52991666); target属性是'Source'(类型'Object')
这听起来像是在寻找CollectionViewSource上的'FooList'而不是绑定到DataTemplate的对象.
那么......我怎么才能看到正确的物体呢?
我正在阅读这篇文章,作者提出了使用DataTemplates定义ViewModel的建议,这是一种疯狂的方式(#7).我一直这样做,真的那么糟糕吗?
<DataTemplate DataType="{x:Type local:MyViewModel}">
<Grid>
...
</Grid>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我的大多数视图都只是一个定义一个或两个DataTemplate的ResourceDictionary.对我而言,这比为每个ViewModel创建UserControl更有意义.为什么我不想在WPF的可视化树中使用额外的图层?当DataTemplate为我做这件事时,为什么我要照顾将ViewModels映射到Views?这种语法真的是一种"疯子式"吗?
这可能是一个愚蠢的问题,但是可以将一些示例数据定义为DataContext,以便在DesignView中查看我的DataTemplate吗?
目前,我总是要运行我的应用程序,看看我的更改是否正常.
例如,使用以下代码,DesignView只显示一个空列表框:
<ListBox x:Name="standardLayoutListBox" ItemsSource="{Binding myListboxItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" Content="{Binding text1}" />
<Label Grid.Column="1" Content="{Binding text2}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud) 我有一个MVVM应用程序,需要在屏幕之间进行基本的向后/向前导航.目前,我已经使用WorkspaceHostViewModel实现了这一点,它跟踪当前工作空间并公开必要的导航命令,如下所示.
public class WorkspaceHostViewModel : ViewModelBase
{
private WorkspaceViewModel _currentWorkspace;
public WorkspaceViewModel CurrentWorkspace
{
get { return this._currentWorkspace; }
set
{
if (this._currentWorkspace == null
|| !this._currentWorkspace.Equals(value))
{
this._currentWorkspace = value;
this.OnPropertyChanged(() => this.CurrentWorkspace);
}
}
}
private LinkedList<WorkspaceViewModel> _navigationHistory;
public ICommand NavigateBackwardCommand { get; set; }
public ICommand NavigateForwardCommand { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我还有一个WorkspaceHostView绑定到WorkspaceHostViewModel,如下所示.
<Window x:Class="MyNavigator.WorkspaceHostViewModel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<ResourceDictionary Source="../../Resources/WorkspaceHostResources.xaml" />
</Window.Resources>
<Grid>
<!-- Current Workspace -->
<ContentControl Content="{Binding Path=CurrentWorkspace}"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
在WorkspaceHostResources.xaml文件中,我将WPF用于使用DataTemplates呈现每个WorkspaceViewModel的View关联起来.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNavigator"> …Run Code Online (Sandbox Code Playgroud) 问题:在可滚动区域中显示大量数据具有可怕的性能和/或用户体验.
尝试:基本上在ListBox中设置DataTemplate以显示填充数据的网格,VirtualizationMode设置为Recycle,并在ListBox上设置固定高度.类似下面的例子.
<ListBox x:Name="Items"
TabNavigation="Once"
VirtualizingStackPanel.VirtualizationMode="Recycling"
Height="500">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,5">
<HyperlinkButton Content="Action" Margin="5"/>
<ContentControl
cal:View.Model="{Binding}"
VerticalContentAlignment="Stretch"
HorizontalContentAlignment="Stretch"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
ContentControl <Grid>将从另一个视图中引入一个标准,该视图格式化由大约20个静态和20个数据绑定TextBlock组成的填充项的整体布局.
这样可以正常工作,并将初始负载减少一半.但是,现在的问题是我需要高度的能力不是一个固定的大小,所以它占用了其父级可用的空间,甚至可以调整大小.感谢@DanFox我发现你必须以一种或另一种形式修复高度以调用虚拟化,或者RenderEngine只是认为它有无限的空间.
问题是:有没有更好的方法来做到这一点,或者我怎样才能至少修复当前的技术以实现更好的用户体验?我正在生成数百个这样的项目,所以我需要虚拟化的性能增强.但是,我还需要允许用户调整窗口大小并保持有效滚动的能力.
非常感谢任何见解,谢谢和节日快乐!
我想构建一个通用/可重用的模式对话框,我可以在我们的WPF(MVVM) - WCF LOB应用程序中使用它.
我有一个视图和相关的ViewModel,我想使用对话框显示.Views和ViewModel之间的绑定是使用以类型为目标的DataTemplates完成的.
以下是我能够起草的一些要求:
做这个的最好方式是什么?
我有一个DataTemplate代表AppBar按钮,我通过自定义AppBarCommand对象的集合声明.
public AppBarCommand(RelayCommand command, string buttonstyle)
{
Command = command;
ButtonStyle = buttonstyle;
}
<DataTemplate>
<Button Command="{Binding Command}"
Style="{Binding ButtonStyle, Converter={StaticResource StringNameToStyleConverter}}"/>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我想添加一个CommandParameter绑定,但参数必须是Button本身.这样我就可以设置Callisto弹出窗口的PlacementTarget.这可能吗?
datatemplate ×10
wpf ×8
xaml ×4
mvvm ×3
binding ×2
c# ×2
silverlight ×2
data-binding ×1
designview ×1
events ×1
listbox ×1
modal-dialog ×1
navigation ×1
triggers ×1
uiscrollview ×1
view ×1