标签: datatemplate

在WPF中,如何从包含ListBox的DataTemplate内部数据绑定到Window DataContext?

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

.net data-binding wpf datatemplate mvvm

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

过滤使用嵌套的xaml数据模板显示的分层对象

我无法过滤嵌套的xaml模板中显示的分层数据.

我有一个ObservableCollection<Foo> Foos,我在XAML中显示.

让我们说Foo看起来像:

class Foo
{
    public ObservableCollection<Bar> Bars;
}

class Bar
{
    public ObservableCollection<Qux> Quxes;
}
Run Code Online (Sandbox Code Playgroud)

我正在使用以下xaml显示Foos:

<Grid>
    <Grid.Resources>
        <CollectionViewSource x:Key="MyCVS" Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.UnifiedSymbols}" Filter="MyCVS_Filter" />

        <DataTemplate x:Key="NestedTabHeaderTemplate">
            <TextBlock Text="{Binding Path=Name}"/>
        </DataTemplate>
        <DataTemplate x:Key="NestedTabContentTemplate">
            <ListBox ItemsSource="{Binding Path=Quxes}" DisplayMemberPath="Name"/>
        </DataTemplate>

        <DataTemplate x:Key="TopLevelTabHeaderTemplate">
            <TextBlock Text="{Binding Path=Name}"/>
        </DataTemplate>
        <DataTemplate x:Key="TopLevelTabContentTemplate">
            <TabControl ItemsSource="{Binding Path=Bars}"
                        ItemTemplate="{StaticResource NestedTabHeaderTemplate}" 
                        ContentTemplate="{StaticResource NestedTabContentTemplate}"
                        />
        </DataTemplate>
    </Grid.Resources>

    <TabControl ItemSource="{Binding correct binding for my control's collection of Foos}"
                ItemTemplate="{StaticResource TopLevelTabHeaderTemplate}" 
                ContentTemplate="{StaticResource TopLevelTabContentTemplate}"
                            x:Name="tabControl"
                /> …
Run Code Online (Sandbox Code Playgroud)

c# xaml binding filtering datatemplate

8
推荐指数
1
解决办法
3371
查看次数

如何使用突出显示的查询术语在WPF项目控件中显示搜索结果

我想在WPF ItemsControl中显示搜索结果,并突出显示查询字词.

我使用的搜索引擎,带有Highlighter插件的Lucene.Net,返回带有标记的查询字符串,如下所示:

...these <Bold>results</Bold> were found to be statistically significant...
Run Code Online (Sandbox Code Playgroud)

我可以指示Highlighter插件使用任何标记标记来包装查询术语.我不限于<Bold>上面例子中的标签.对于WPF,我可能会使这些<Run/>元素附加一个样式.

挑战是获取我已经给出的字符串并将其呈现为我在搜索结果中使用的数据模板中的"实际XAML".换句话说,我想看到这样的事情:

......这些结果被发现具有统计学意义......

但我正在努力解决如何在数据模板中将数据绑定与XAML字符串的动态呈现相结合的问题.这里最好的方法是什么?

  1. 使用UserControl显示每个搜索结果并XamlReader.Load()从代码隐藏调用?
  2. 构造包含搜索结果字符串的FlowDocument并使用FlowDocumentScrollViewer显示结果?
  3. 还有其他东西......?

wpf xaml datatemplate itemscontrol

8
推荐指数
3
解决办法
9547
查看次数

如何使用TextBox作为TreeViewItems通过ListView进行TAB?

所以我基本上有这个ListView,我想按Tab并迭代我的TreeViewItems(最好只我的TextBoxes)

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn  Header="number"  />
            <GridViewColumn Header="Selector">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding SelectorName}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)

我看到的情况是在第一次按下选项卡后选择了整个第一个TreeViewItem并再次按Tab键选择了第一个TextBox.最后,第三个TAB从TreeView中移出到下一个Control,尽管还有更多TextBox我想在"Tabing"之前赶到下一个Control.Thankx

编辑:问题在这里得到解答: 如何通过ListView中的TextBox来选择

wpf xaml listview textbox datatemplate

8
推荐指数
2
解决办法
5009
查看次数

WPF DataTemplate ContentPresenter"绑定"

有人可以解释一下我在这里使用的代码是如何工作的吗?

<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)

我目前在细节方面的问题是:

  • 为什么ContentPresenter可以在不引用不同DataTemplates的情况下呈现正确的UserControl?我可以看到,ContentPresenter内容绑定到我的ViewModels CurrentViewProperty,但我的DataTemplates没有?
  • 另一个很棒的功能是UserControls使用正确的ViewModels而没有声明.(或者我没看到声明)

我已经找到了这个描述http://msdn.microsoft.com/en-us/library/System.Windows.Controls.ContentPresenter(v=vs.110).aspx但是备注部分没有回答这个问题.(或者我看不到他们......)

再次,为了清楚起见,一切都很完美,但我不明白为什么,所以这只是一个理解选择模板和绑定的问题.

wpf datatemplate

8
推荐指数
2
解决办法
8571
查看次数

UWP DataTemplates用于ListView中的多个项类型

我将如何实现这一目标?

让我们说这是我的模型:

public interface IAnimal
{
     string Name { get; }
}
public class Fish : IAnimal
{
    public string Name { get; set; }
    public int ScalesCount { get; set; }
}
public class Dog : IAnimal
{
    public string Name { get; set; }
    public string CollarManufacturerName { get; set; }
}

public class ViewModel
{
    public ObservableCollection<IAnimal> Animals { get; set; }

    public ViewModel()
    {
        this.Animals = new ObservableCollection<IAnimal>();
        this.Animals.Add(new Fish { Name = "Carl", ScalesCount = …
Run Code Online (Sandbox Code Playgroud)

xaml listview datatemplate win-universal-app

8
推荐指数
1
解决办法
4478
查看次数

在MVVM中,DataTemplates认为视图是UserControls是视图吗?

在MVVM中,每个View都有一个ViewModel.一个视图我理解为一个Window,Page或UserControl,你可以附加一个ViewModel,视图从中获取其数据.

DataTemplate也可以呈现ViewModel的数据.

所以我理解DataTemplate是另一个"View",但似乎存在差异,例如Windows,Pages和UserControls可以定义自己的.dll,一个类型与DataContect绑定另一个通过附加模板使Windows,Pages ,UserControls可以通过ServiceLocator/Container等动态附加到ViewModels .

当在UI上呈现ViewModel的数据时,DataTemplates与Windows/Pages/UserControls有何不同?除了这四种之外还有其他类型的"观点"吗?

.net wpf datatemplate mvvm viewmodel

7
推荐指数
1
解决办法
5014
查看次数

无法通过DataTrigger设置ContentTemplate

我希望它ContentTemplate根据中的值而变化DataTrigger.
是的,我考虑使用a DataTemplateSelector,但现在我需要一个DataTrigger或更好的说a MultiDataTrigger.

请看下面的示例应用程序,DataTemplate不会改变:

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:WpfApplication1">
    <StackPanel>
        <CheckBox IsChecked="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Content="BoolProperty"/>
        <ContentControl Content="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}">
            <ContentControl.ContentTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Content="Template 1"/>
                </DataTemplate>
            </ContentControl.ContentTemplate>
            <ContentControl.Resources>
                <DataTemplate x:Key="Template2">
                    <CheckBox IsChecked="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Content="Template 2"/>
                </DataTemplate>
            </ContentControl.Resources>
            <ContentControl.Style>
                <Style TargetType="ContentControl">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding BoolProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:Window1}}}" Value="True">
                            <Setter Property="ContentTemplate" Value="{StaticResource Template2}"/>
                        </DataTrigger> …
Run Code Online (Sandbox Code Playgroud)

wpf binding datatrigger datatemplate contentcontrol

7
推荐指数
2
解决办法
8731
查看次数

带有默认模板和itemTemplate的递归菜单项

我有一个MenuItem类型的WPF控件模板:

<Style TargetType="{x:Type MenuItem}">
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="Cursor"
            Value="Hand" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MenuItem}">
                <Border Background="{TemplateBinding Background}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <ContentControl Content="{TemplateBinding Header}"
                                        Margin="5"
                                        Grid.Column="1" />
                        <Path Grid.Column="2"
                                x:Name="Indicator"
                                Data="M1,1 L1,9 9,5Z"
                                Fill="{StaticResource GlyphBrush}"
                                Margin="4"
                                Visibility="Hidden"
                                VerticalAlignment="Center" />
                        <Popup Name="PART_Popup"
                                Placement="Right"
                                IsOpen="{TemplateBinding IsSubmenuOpen}"
                                AllowsTransparency="True"
                                Grid.Column="0"
                                Grid.ColumnSpan="2"
                                HorizontalOffset="3"
                                VerticalOffset="-1">
                            <Border Background="Transparent">
                                <ContentControl Style="{StaticResource PopupContentStyle}">
                                    <ItemsPresenter/>
                                </ContentControl>
                            </Border>
                        </Popup>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked"
                                Value="true">
                        <Setter Property="Background" …
Run Code Online (Sandbox Code Playgroud)

wpf datatemplate menuitem controltemplate

7
推荐指数
1
解决办法
847
查看次数

当从DataTemplateSelector显式返回DataTemplate时,为什么DataTemplate不能绑定到接口?

我已经创建了一个DataTemplateSelector,它使用已知接口的集合进行初始化.如果传入选择器的项目实现其中一个接口,则返回关联的数据模板.

首先,这是有问题的ICategory接口......

public interface ICategory
{
    ICategory ParentCategory { get; set; }
    string    Name           { get; set; }

    ICategoryCollection Subcategories { get; }
}
Run Code Online (Sandbox Code Playgroud)

这是基于基类或接口而不仅仅是特定具体类匹配的DataTemplateSelector ...

[ContentProperty("BaseTypeMappings")]
public class SubclassedTypeTemplateSelector : DataTemplateSelector
{
    private delegate object TryFindResourceDelegate(object key);

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var frameworkElement = container as FrameworkElement;

        foreach(var baseTypeMapping in BaseTypeMappings)
        {
            // Check if the item is an instance of, a subclass of,
            // or implements the interface specified in BaseType
            if(baseTypeMapping.BaseType.IsInstanceOfType(item))
            {
                // …
Run Code Online (Sandbox Code Playgroud)

c# wpf datatemplate hierarchicaldatatemplate datatemplateselector

7
推荐指数
1
解决办法
761
查看次数