小编Wal*_*mer的帖子

编号列表框

我有一个已排序的列表框,需要显示每个项目的行号.在这个演示中,我有一个带有Name字符串属性的Person类.列表框显示按名称排序的人员列表.如何添加到列表框的datatemplate行号?

XAML:

<Window x:Class="NumberedListBox.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <ListBox 
        ItemsSource="{Binding Path=PersonsListCollectionView}" 
        HorizontalContentAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Name}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>
Run Code Online (Sandbox Code Playgroud)

代码背后:

using System;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows;
using System.ComponentModel;

namespace NumberedListBox
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            Persons = new ObservableCollection<Person>();
            Persons.Add(new Person() { Name = "Sally"});
            Persons.Add(new Person() { Name = "Bob" });
            Persons.Add(new Person() { Name = "Joe" });
            Persons.Add(new Person() { Name = "Mary" });

            PersonsListCollectionView = …
Run Code Online (Sandbox Code Playgroud)

c# wpf listbox

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

多个用户控件共享集合依赖项属性

我已经基于列表框实现了自己的usercontrol.它具有一个具有集合类型的依赖项属性.当我在窗口中只有一个usercontrol实例时,它工作正常,但如果我有多个实例,我会遇到问题,他们共享集合依赖项属性.以下是说明这一点的示例.

我的用户控件名为SimpleList:

<UserControl x:Class="ItemsTest.SimpleList"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Name="_simpleList">
    <StackPanel>
        <TextBlock Text="{Binding Path=Title, ElementName=_simpleList}" />
        <ListBox 
            ItemsSource="{Binding Path=Numbers, ElementName=_simpleList}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
        </ListBox>
    </StackPanel>    
</UserControl>
Run Code Online (Sandbox Code Playgroud)

代码背后:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace ItemsTest
{
    public partial class SimpleList : UserControl
    {
        public SimpleList()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(SimpleList), new UIPropertyMetadata(""));


        public List<int> Numbers 
        {
            get …
Run Code Online (Sandbox Code Playgroud)

wpf dependency-properties itemscontrol

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

在代码中添加项目时,使用ItemTemplate进行TreeView

我在后面的代码中手动添加TreeViewItems,并希望使用DataTemplate来显示它们,但无法弄清楚如何.我希望做这样的事情,但项目显示为空标题.我究竟做错了什么?

XAML

<Window x:Class="TreeTest.WindowTree"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WindowTree" Height="300" Width="300">
    <Grid>
        <TreeView Name="_treeView">
            <TreeView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name}" />
                        <TextBlock Text="{Binding Path=Age}" />
                    </StackPanel>
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

代码背后

using System.Windows;
using System.Windows.Controls;

namespace TreeTest
{
    public partial class WindowTree : Window
    {
        public WindowTree()
        {
            InitializeComponent();

            TreeViewItem itemBob = new TreeViewItem();
            itemBob.DataContext = new Person() { Name = "Bob", Age = 34 };

            TreeViewItem itemSally = new TreeViewItem();
            itemSally.DataContext = new Person() { Name = "Sally", Age …
Run Code Online (Sandbox Code Playgroud)

wpf treeview xaml datatemplate itemtemplate

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

WPF - 使用MVVM基于模型的不同状态禁用和启用不同控件的优雅方法

我正在为以下问题寻找一个优雅的解决方案.

假设我们有一个具有以下布尔属性的(View)模型:

  • Α
  • Beta版
  • 伽玛
  • 三角洲

接下来,我在表面上有5个控件,只有在满足基于这些属性的条件时才能看到它们.当然,只要更新其中一个属性,就应该立即传播更改:

  • ControlA - > Alpha &&(Beta || Gamma)
  • ControlB - > Delta
  • ControlC - > Delta || Beta版
  • ControlD - > Gamma && Alpha && Delta
  • ControlE - > Alpha || 伽玛

到目前为止我提出的唯一解决方案是使用MultiValueConverters.

ControlA的示例:

<ControlA>
   <ControlA.Visibility>
      <MultiBinding Converter={StaticResource ControlAVisibilityConverter}>
          <Binding Path="Alpha"/>
          <Binding Path="Beta"/>
          <Binding Path="Gamma"/>
      </MultiBinding>
   </ControlA.Visibility>
</ControlA>
Run Code Online (Sandbox Code Playgroud)

此ControlAVisibilityConverter检查条件"Alpha &&(Beta || Gamma)"并返回适当的值.

它确实有用......好吧..但也许你可以想出一个更优雅的解决方案?

谢谢TwinHabit

.net wpf converter mvvm multibinding

6
推荐指数
1
解决办法
6193
查看次数

覆盖 ScrollViewer 的默认控件模板时,VirtualizingStackPanel 停止工作

我有一个列表框,里面有很多渲染成本很高的项目。然而,VirtualizingStackPanel 通过只渲染可见项来处理这个问题。我想覆盖 ScrollViewer 的控件模板,因为默认的控件模板在水平和垂直滚动条之间有灰色矩形。我只是复制了微软提供的一个(ScrollViewer ControlTemplate Example),它没有灰色矩形问题。

然而,这个控制模板通过给 VirtualizingStackPanel 无限的高度来禁用虚拟化。这意味着 VirtualizingStackPanel 将呈现所有项目,因为它认为所有项目都是可见的。

在下面的演示代码中,我在列表框中显示了 10000 个项目。我通过比较运行它与 ScrollViewer 样式和没有它来验证问题。有了它,演示运行得非常慢,调整大小需要几秒钟。没有样式它非常快。我输出了一些关于 VirtualizingStackPanel 的信息来证明我的观点:

没有 ScrollViewer 样式(注释掉 XAML 中的样式):

ViewportHeight: 8
ExtentHeight: 10000
ActualHeight: 245
IsVirtualizing: True
VirtualizationMode: Standard

使用 ScrollViewer 样式:

ViewportHeight: 0
ExtentHeight: 0
ActualHeight: 272766.666666707
IsVirtualizing: True
VirtualizationMode: Standard

知道如何为不会与虚拟化混淆的 ScrollViewer 编写控件模板吗?

XAML:

<Window x:Class="VirtualTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>

        <Style x:Key="{x:Type ScrollViewer}" TargetType="{x:Type ScrollViewer}">
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition …
Run Code Online (Sandbox Code Playgroud)

wpf scrollviewer controltemplate virtualizingstackpanel

4
推荐指数
1
解决办法
4100
查看次数

用户控件的Func属性

我有一个用户控件,我想添加一个Func类型的依赖项属性,所以我可以在XAML中为它分配一个方法处理程序.但是,这将导致XAMLParseException:'Func`2'类型没有公共TypeConverter类.我究竟做错了什么?我需要为Func实现TypeConverter还是有更好的方法?

用户控件中的Func依赖项属性(MyUserControl):

public Func<int, int> MyFunc
{
    get { return (Func<int, int>)GetValue(MyFuncProperty); }
    set { SetValue(MyFuncProperty, value); }
}

public static readonly DependencyProperty MyFuncProperty =
    DependencyProperty.Register("MyFunc", 
                                typeof(Func<int, int>), 
                                typeof(SillyCtrl), 
                                new UIPropertyMetadata(null));
Run Code Online (Sandbox Code Playgroud)

使用DP,XAML的示例:

<Window x:Class="FuncTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:FuncTest="clr-namespace:FuncTest"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <FuncTest:MyUserControl MyFunc="SquareHandler" />
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

代码背后:

namespace FuncTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            SquareHandler = (arg => arg * arg);

            DataContext = this;
        }

        public Func<int, int> SquareHandler { get; set; } …
Run Code Online (Sandbox Code Playgroud)

wpf user-controls dependency-properties

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