标签: datatemplate

是否可以在Silverlight DataTemplate中绑定事件?

是否可以在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"中加入什么来捕捉事件?

谢谢,杰夫

silverlight events binding datatemplate

12
推荐指数
1
解决办法
9623
查看次数

如何在WPF中分解出DataTemplate的绑定?

我有一个DataTemplate我想重用.我要分解的部分是绑定,因为它是唯一改变的东西.我DataTemplate看起来大致像这样.(实际上还有更多的东西,但我已经把外来的东西拿走了.)

<DataTemplate>
    <TextBox Text="{Binding Name}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

如何DataTemplate简单地改变我绑定的属性?(请注意,如果它只是一个简单TextBox,我不会担心它,但DataTemplate实际上包含一个StackPane带有许多其他元素的l.我想将它集中在一个地方,因此DataTemplate.)

我想过两种解决这个问题的方法.

  1. 创建一个简单的自定义控件 重复使用,不要担心重复使用DataTemplate.
  2. 尝试使用某种DataTemplate子类.(我被告知这是可能的.)我将向它添加一个依赖属性,它允许我指定要绑定的属性的名称.

建议?

wpf xaml datatemplate

12
推荐指数
1
解决办法
1528
查看次数

在内部ComboBox聚焦时选择ListBoxItem

我有一个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)

wpf xaml triggers listbox datatemplate

12
推荐指数
2
解决办法
5800
查看次数

在DataTemplate中绑定CollectionViewSource

'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的对象.

那么......我怎么才能看到正确的物体呢?

data-binding wpf datatemplate collectionviewsource

12
推荐指数
2
解决办法
5968
查看次数

我应该将UserControl用于我的视图而不是DataTemplates吗?

我正在阅读这篇文章,作者提出了使用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?这种语法真的是一种"疯子式"吗?

wpf user-controls view datatemplate mvvm

12
推荐指数
1
解决办法
2262
查看次数

在xaml中设计datatemplate的时间数据

这可能是一个愚蠢的问题,但是可以将一些示例数据定义为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)

wpf xaml datatemplate designview

12
推荐指数
1
解决办法
1万
查看次数

如何在MVVM应用程序中的视图之间导航时保​​留视图的完整状态?

我有一个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)

navigation wpf datatemplate mvvm

12
推荐指数
2
解决办法
7028
查看次数

大型可滚动数据SL4的虚拟化性能问题

问题:在可滚动区域中显示大量数据具有可怕的性能和/或用户体验.

尝试:基本上在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只是认为它有无限的空间.

问题是:有没有更好的方法来做到这一点,或者我怎样才能至少修复当前的技术以实现更好的用户体验?我正在生成数百个这样的项目,所以我需要虚拟化的性能增强.但是,我还需要允许用户调整窗口大小并保持有效滚动的能力.

非常感谢任何见解,谢谢和节日快乐!

c# silverlight xaml datatemplate uiscrollview

12
推荐指数
1
解决办法
461
查看次数

如何在MVVM之后为WPF构建通用/可重用的模式对话框

我想构建一个通用/可重用的模式对话框,我可以在我们的WPF(MVVM) - WCF LOB应用程序中使用它.

我有一个视图和相关的ViewModel,我想使用对话框显示.Views和ViewModel之间的绑定是使用以类型为目标的DataTemplates完成的.

以下是我能够起草的一些要求:

  • 我更喜欢这个基于Window而不是使用Adorners和控件,它们就像一个模态对话框.
  • 它应该从内容中获得最小尺寸.
  • 它应该以所有者窗口为中心.
  • 窗口不得显示"最小化"和"最大化"按钮.
  • 它应该从内容中获得它的标题.

做这个的最好方式是什么?

wpf modal-dialog datatemplate mvvm

11
推荐指数
2
解决办法
4794
查看次数

将WPF Button CommandParameter绑定到DataTemplate中的Button本身

我有一个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本身.这样我就可以设置Call​​isto弹出窗口的PlacementTarget.这可能吗?

c# wpf binding datatemplate commandparameter

11
推荐指数
2
解决办法
3万
查看次数