0
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의 용도입니다. 데이터 항목 (Model)을 적절한 ViewModel에 랩핑하고 DataBinding을 사용하여 UI를 조작합니다. –