【问题标题】:Items Template code-behind项目模板代码隐藏
【发布时间】:2014-04-07 09:32:07
【问题描述】:

有没有办法访问为 itemscontrol 的项目生成的容器?

例如, 给定一个带有 ItemTemplate 的 ItemsControl,如下所示,访问为控件中的每个项目生成的 DataTemplate 中的实际 TextBlock。 (不是对象,而是与其关联的文本块)。

视图/MainPage.xaml:

<phone:PhoneApplicationPage
    x:Class="PhoneApp1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="clr-namespace:PhoneApp1.ViewModels"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.    Toolkit"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <phone:Pivot>
        <phone:Pivot.Resources>
            <vm:PersonViewModel x:Key="ViewModel"/>
        </phone:Pivot.Resources>

        <phone:PivotItem>
            <phone:PivotItem.Header>
                <TextBlock Text="Pivot"/>
            </phone:PivotItem.Header>

            <ItemsControl x:Name="People" DataContext="{StaticResource ViewModel}"     ItemsSource="{Binding People}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock toolkit:SlideInEffect.LineIndex="{Binding PersonLineIndex}"     Text="{Binding Name}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </phone:PivotItem>

        <phone:PivotItem>
            <phone:PivotItem.Header>
                <TextBlock Text="Empty Pivot"/>
            </phone:PivotItem.Header>
        </phone:PivotItem>
    </phone:Pivot>

</phone:PhoneApplicationPage>

视图/MainPage.xaml.cs

using Microsoft.Phone.Controls;

namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }
    }
}

ViewModels/PersonViewModel.cs

using PhoneApp1.Models;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace PhoneApp1.ViewModels
{
    public class PersonViewModel
    {
        public PersonViewModel()
        {
            People = new ObservableCollection<Person>
            {
                new Person { Name = "Joe" },
                new Person { Name = "Jack" },
                new Person { Name = "James" },
                new Person { Name = "John" }
            };

            PersonLineIndex = new List<int>();
            for (int i = 0; i < People.Count; i++)
            {
                PersonLineIndex.Add(i);
            }
        }

        public ObservableCollection<Person> People { get; set; }

        public List<int> PersonLineIndex { get; set; }
    }
}

模型/Person.cs:

namespace PhoneApp1.Models
{
    public class Person
    {
        public string Name { get; set; }
    }
}

我为什么需要此访问权限?例如: 尝试为每个文本块设置不同的行索引。不向您的 Person 添加“LineIndex”属性(因为这会违反 MVVM)。

【问题讨论】:

  • 你要那个干什么?
  • 你好@HighCore,因为我需要为每个人单独更改文本块中的一个属性,并且我不想通过简单地将该属性添加到人和绑定来违反模型和视图之间的 MVVM 分离它到每个文本块。
  • 这就是 ViewModel 的用途。将您的数据项(模型)包装在适当的 ViewModel 中,并使用 DataBinding 来操作 UI。
  • 我不认为你理解这个问题,这里有一个例子:尝试为每个生成的文本块设置不同的字体,例如字体。不向您的 Person 添加“字体”属性,因为这会违反 MVVM。
  • @FranciscoAguilera 在这种情况下,您将绑定到可以转换为字体的视图模型上的属性,然后使用转换器。

标签: c# xaml datatemplate


【解决方案1】:

我想通了。您将访问此类 ItemsControl 的 ItemContainerGenerator

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        People.Loaded += People_Loaded;
    }

    void People_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        for (int i = 0; i < People.Items.Count; i++)
        {
            var container = People.ItemContainerGenerator.ContainerFromIndex(i);
            container.SetValue(SlideInEffect.LineIndexProperty, i);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多