标签: datatemplate

专注于DataTemplate中的TextBox

我有DataTemplate一个TextBox.我将此模板设置为选择中的列表框项.

我无法将焦点设置到模板中的文本框中.我试图调用MyTemplate.FindName,但最终会出现无效操作异常:此操作仅对应用了此模板的元素有效.

我该如何访问它?

wpf textbox focus datatemplate

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

带有DataTemplates的WPF ListBoxItems - 如何从DataTemplate中引用绑定到ListBoxItem的CLR对象?

我有一个ListBox,它绑定了一个ObservableCollection.

每个ListBoxItem都显示一个DataTemplate.我有一个按钮DataTemplate,当单击时,需要引用ObservableCollection它的部分DataTemplate的成员.我无法使用该ListBox.SelectedItem属性,因为单击该按钮时该项目未被选中.

所以要么:我需要弄清楚ListBox.SelectedItem当鼠标悬停时,或者单击按钮时如何正确设置.或者我需要找出另一种方法来获取ListBoxItem对该按钮所属的CLR对象的引用.第二个选项看起来更干净,但无论哪种方式都可以.

简化的代码段如下:

XAML:

<DataTemplate x:Key="postBody">
    <Grid>
        <TextBlock Text="{Binding Path=author}"/>
        <Button Click="DeleteButton_Click">Delete</Button>
    </Grid>
</DataTemplate>

<ListBox ItemTemplate="{StaticResource postBody}"/>
Run Code Online (Sandbox Code Playgroud)

C#:

private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
    Console.WriteLine("Where mah ListBoxItem?");
}
Run Code Online (Sandbox Code Playgroud)

wpf listbox datatemplate listboxitem

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

WPF内存使用情况

应用:

  • WPF应用程序由顶部的文本框和下面的列表框组成
  • 用户在TextBox中键入字符串以查找员工,搜索结果显示在ListBox中
  • ListBox使用DataTemplates显示元素(显示员工姓名,部门,电话和缩略图.)

执行:

  • 在应用程序启动时,我查询数据库并检索要在ListBox中显示的所有员工和相关信息.这一直保存在内存中.
  • 应用程序启动后,所有可搜索的数据都在内存中,搜索几乎是即时的.所有搜索都在内存中的数据上执行.
  • 搜索结果使用DataTemplates显示在ListBox中.缩略图图片,名称,电话,部门等显示在每个ListBox项目中.

问题:

  • 启动时,内存使用量约为200MB.
  • 随着列表框中的数据更改,无论是通过新搜索还是简单地向下滚动列表框,内存消耗都会增加.
  • 当用户慢慢向下滚动列表框时,内存增加得更快.当你上下滚动时,内存很快会达到1GB.

没有代码手动创建控件 - 一切都是通过数据绑定完成的.

为什么我看到这种行为?我该怎么办才能修复它?请帮忙!

更新:我发现问题不是内存泄漏.这里的问题是列表框正在创建对象以显示员工的图像,并且在listboxitem离开窗口后不会为垃圾收集器发布.CleanUpVirtualizedItem事件按我的预期发生,但内存仍未释放.有任何想法吗?

wpf performance memory-management listbox datatemplate

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

如何将ObservableCollection绑定到DataTemplate中的TextBoxes?

我试图成功地将一个ObservableCollection绑定到DataTemplate中的TextBoxes.我可以正确显示数据,但我无法通过UI更改列表数据.我有一个名为'model'的Model类,它包含一个名为'List'的ObservableCollection.该类实现了INotifyPropertyChanged接口.这是shell的xaml.Window1网格的DataContext设置为"theGrid.DataContext = model"

<Window x:Class="BindThat.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:BindThat"
Title="Window1" Height="300" Width="300">
<StackPanel x:Name="theGrid">
    <GroupBox BorderBrush="LightGreen">
        <GroupBox.Header>
            <TextBlock Text="Group" />
        </GroupBox.Header>
        <ItemsControl ItemsSource="{Binding Path=List}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </GroupBox> 
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

这是Model类的代码:

class Model : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    private ObservableCollection<string> _list = new ObservableCollection<string>();
    public ObservableCollection<string> List
    {
        get { return _list; }
        set 
        { 
            _list = value;
            NotifyPropertyChanged("List"); …
Run Code Online (Sandbox Code Playgroud)

wpf binding datatemplate two-way-binding

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

C#/ WPF:获取DataTemplate中元素的绑定路径

如何在DataTemplate中获取元素的绑定路径?我的XAML看起来像这样:

<GridViewColumn Header="Double">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding TotalValues, Mode=OneWay, StringFormat=\{0:0\'0.00\}, Converter={StaticResource GridValueConverter}}" TextAlignment="Right" Width="auto"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Comments" DisplayMemberBinding="{Binding Path=Comments, Mode=OneWay}" Width="auto"/>
Run Code Online (Sandbox Code Playgroud)

获取"普通"GridViewColumnHeader.DisplayMemberBinding的绑定路径是

var field = (string)((Binding)((GridViewColumnHeader)e.OriginalSource).Column.DisplayMemberBinding).Path.Path;
Run Code Online (Sandbox Code Playgroud)

如何才能获得相同的绑定路径TextBlock.Text

c# wpf binding datatemplate

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

使用自定义类型扩展名指定DataTemplate.DataType

我有这个扩展标记

public class NullableExtension : TypeExtension
{
    public NullableExtension() {
    }

    public NullableExtension( string type )
        : base(type) {
    }

    public NullableExtension( Type type )
        : base(type) {
    }

    public override object ProvideValue( IServiceProvider serviceProvider ) {
        Type basis = (Type)base.ProvideValue( serviceProvider );
        return typeof(Nullable<>).MakeGenericType( basis );
    }
}
Run Code Online (Sandbox Code Playgroud)

它旨在提供其他类型的可空版本.当在"普通"XAML中使用时,它按预期工作.例如,如

<SomeControl DataContext="{My:Nullable System:Int32}"/>
Run Code Online (Sandbox Code Playgroud)

(假设My是为保存扩展的C#名称空间定义的XML名称空间,对于System来说类似).控件的数据上下文设置为System.Typefor,Nullable<int>如我所料.

但是,当我使用此扩展来尝试设置类似的DataType属性时DataTemplate

<DataTemplate DataType="{My:Nullable System:Int32}">
  <TextBlock ... />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

编译器告诉我的是

字典的键不能是'System.Windows.Controls.Primitives.TextBlock'类型.只支持String,TypeExtension和StaticExtension."

"类型'NullableExtension'的构造函数没有1个参数.

有人知道为什么只允许这三种方法(甚至不是TypeExtension像我的子类一样)?那时处理XAML有什么特别之处?是否有另一种方法来实现这一点(基于可以为空的类型的数据模板选择)而不诉诸DataTemplateSelector

wpf xaml datatemplate

9
推荐指数
1
解决办法
5959
查看次数

ListView:在资源字典中定义ItemsPanelTemplate

我有一个ListView,其布局看起来像一个Windows资源管理器视图(图标+一些细节),绑定到ViewModel中的某个列表.

我的目标是能够随时在资源管理器视图或经典视图之间切换.

我可以ItemsPanelTemplate直接在ListView.ItemsPanel字段中定义完全正确地显示布局的工作.现在,我想在资源中定义它,以便我能够在不同的视图中使用它,特别是在一个控件中,用户应该在Explorer视图或经典列表视图之间进行选择(默认渲染为一个列表)

你怎么做的?我无法定义任何ItemsPanelTemplate在我的ResourceDictionary,如果我定义DataTemplate它是不兼容的(虽然我认为遵循纯逻辑,ItemsPanelTemplate应该继承DataTemplate,但它实际上不是这样).

实际列表的代码段:

<ListView SelectionMode="Single"
                VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Bottom"
                ScrollViewer.VerticalScrollBarVisibility="Auto"
                ItemsSource="{Binding ListUserApps, 
                                UpdateSourceTrigger=PropertyChanged}" 
                SelectedIndex="{Binding SelectedUserApp, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Background="White">

                <ListView.ItemsPanel>
                    <ItemsPanelTemplate >
                        <WrapPanel Width="{Binding (FrameworkElement.ActualWidth),
                     RelativeSource={RelativeSource 
                                     AncestorType=ScrollContentPresenter}}"
                     ItemWidth="{Binding (ListView.View).ItemWidth,
                     RelativeSource={RelativeSource AncestorType=ListView}}"

                     ItemHeight="{Binding (ListView.View).ItemHeight,
                     RelativeSource={RelativeSource AncestorType=ListView}}" />
                        <!--MinWidth="{Binding ItemWidth,
                     RelativeSource={RelativeSource Self}}"-->
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="Auto" Width="150">
                            <Image Source="{Binding Path=Appli.AppType, Converter={StaticResource TypeToIconConverter}}" Margin="5"
                                   Height="50" Width="50"/>
                            <StackPanel VerticalAlignment="Center" Width="90">
                                <TextBlock Text="{Binding Path=Appli.AppName}" 
                     FontSize="13" HorizontalAlignment="Left" TextWrapping="WrapWithOverflow" …
Run Code Online (Sandbox Code Playgroud)

wpf listview datatemplate mvvm itemspanel

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

9
推荐指数
2
解决办法
8076
查看次数

WPF TabControl和DataTemplates

我有一组ViewModel,我绑定到TabControl的ItemsSource属性.让我们调用那些ViewModels AViewModel,BViewModel和CViewModel.其中每个都需要有不同的ItemTemplate(对于标题;因为它们每个都需要显示不同的图标)和不同的ContentTemplate(因为它们具有非常不同的交互模型).

我想要的是这样的:

在某处的Resource.xaml文件中定义:

<DataTemplate x:Key="ItemTemplate" DataType="{x:Type AViewModel}">
    ...
</DataTemplate>

<DataTemplate x:Key="ItemTemplate" DataType="{x:Type BViewModel}">
    ...
</DataTemplate>

<DataTemplate x:Key="ItemTemplate" DataType="{x:Type CViewModel}">
    ...
</DataTemplate>

<DataTemplate x:Key="ContentTemplate" DataType="{x:Type AViewModel}">
    ...
</DataTemplate>

<DataTemplate x:Key="ContentTemplate" DataType="{x:Type BViewModel}">
    ...
</DataTemplate>

<DataTemplate x:Key="ContentTemplate" DataType="{x:Type CViewModel}">
    ...
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

单独定义:

<TabControl ItemTemplate="[ Some way to select "ItemTemplate" based on the type ]"
            ContentTemplate="[ Some way to select "ContentTemplate" based on the type ]"/>
Run Code Online (Sandbox Code Playgroud)

现在,我真实地知道,每次我使用相同的键定义DataTemplate时,系统就会抱怨.但是,有什么我可以做的类似于这将允许我将DataTemplate放入基于名称和数据类型的TabControl?

wpf tabcontrol datatemplate

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

WPF:TextBox文本未更新

我有我使用内部的用户控件DataTemplate,这UserControl包含了TextBox其绑定与价值属性(声明为DependencyProperty我的)UserControl.在数据模板中,我将此Value属性与我的实际属性绑定为Name(也是a DependencyProperty).它工作正常,我在加载时为Name属性赋值,我TextBox显示它,我更改了值TextBox,它也更新了我的Name属性.现在问题出现在我PropertyChangedEventHandler在依赖项属性中添加一个,我检查它是否有效的值,如果有效则不执行任何操作,如果无效则分配旧值.在值无效的情况下,当我给它分配旧的价值,性能更新,但它不是在我的显示更新值TextBoxUserControl.谁能告诉我为什么?

我的XAMLUserControl:

<UserControl x:Name="usercontrol">
   <StackPanel>
      <TextBlock ......./>
      <TextBox Binding={Binding ElementName=usercontrol, Path=Value, Mode=TwoWay}/>
   </StackPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

在代码后面,Value是一个DependencyProperty

我在我的使用中DataTemplate:

<HierarchicalDataTemplate DataType="{x:Type myLib:myClass}" ItemsSource="{Binding Children}">
    <Expander Header="{Binding}">
        <WrapPanel>
            <edproperty:TextPropertyEditor Caption="Name" Value="{Binding Name, Mode=TwoWay}"/>
            <edproperty:TextPropertyEditor .................../>
            <edproperty:TextPropertyEditor .................../>
            <edproperty:TextPropertyEditor ...................../>
        </WrapPanel>
    </Expander>
</HierarchicalDataTemplate>
Run Code Online (Sandbox Code Playgroud)

我正在使用的这个模板TreeView …

wpf xaml user-controls textbox datatemplate

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