2012-09-26 3 views
1

XAML을 처음 사용했습니다. ItemsControl에 대해 조사한 결과 쉽게 이해할 수있는 튜토리얼을 발견했지만 문제는 WinRT에서 작동하지 않는다는 것입니다.WinRT Items 컨트롤 (그리드 열과 행 포함)

자습서 : https://rachel53461.wordpress.com/2011/09/17/wpf-itemscontrol-example/ 내가 Style 태그에 TargetType을 사용하려고

그러나, 런타임에서 나는 예외를 얻었다.

<ItemsControl ItemsSource="{Binding MyCollection}"> 
    <!-- ItemsPanelTemplate --> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition /> 
        <RowDefinition /> 
        <RowDefinition /> 
        <RowDefinition /> 
        <RowDefinition /> 
        <RowDefinition /> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition /> 
        <ColumnDefinition /> 
        <ColumnDefinition /> 
        <ColumnDefinition /> 
        <ColumnDefinition /> 
        <ColumnDefinition /> 
       </Grid.ColumnDefinitions> 
      </Grid> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 

    <!-- ItemContainerStyle --> 
    <ItemsControl.ItemContainerStyle> 
     <Style TargetType="TextBox"> 
      <Setter Property="Grid.Column" 
       Value="{Binding xIndex}" /> 
      <Setter Property="Grid.Row" 
       Value="{Binding yIndex}" /> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 

    <!-- ItemTemplate --> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <TextBox Background="{Binding color}" Text="{Binding xIndex,Mode=OneWay}" /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
+1

예외의 세부 사항을 추가하십시오. – akton

답변

3

귀하의 문제는 여기에 있습니다 : 특정 항목이 추가되지 않는 한

ItemsControl에 대한
<Style TargetType="ContentPresenter"> 

ItemContainer의 (a ContentPresenter입니다 :

<Style TargetType="TextBox"> 

이 있어야한다 무엇이다 ItemsControl).

그래서 당신의 뷰 계층이 같은 (당신이 StackPanel 이외의 다른 ItemsPanel를 변경하지 않은 가정)을 찾습니다

<StackPanel> 
    <ContentPresenter> 
     <TextBox/> 
    </ContentPresenter> 
</StackPanel> 

편집 :

스콧 코멘트에서 지적

, 이 솔루션은 실제로 WinRT에서 작동하지 않습니다. 나는 비슷한 일을했고, 당신은 아마 바인딩 해당 할 수정할 수 있습니다

public class CustomItemsControl : ItemsControl 
{ 
    protected override void PrepareContainerForItemOverride(DependencyObject element, object item) 
    { 
     base.PrepareContainerForItemOverride(element, item); 
     FrameworkElement source = element as FrameworkElement; 
     if (source != null) 
     { 
      source.SetBinding(Canvas.LeftProperty, new Binding { Path = new PropertyPath("X"), Mode = BindingMode.TwoWay }); 
      source.SetBinding(Canvas.TopProperty, new Binding { Path = new PropertyPath("Y"), Mode = BindingMode.TwoWay }); 
     } 
    } 
} 

이 컬렉션의 각 항목에 X 재산과 유사하게 Canvas.TopPropertyY 속성에 Canvas.LeftProperty을 결합한다.

+0

예외없이 실행되지만 column 및 row 속성을 아직 사용하지 않습니다. – user1701616

+0

이제는 각 요소에 여백이있는 캔버스를 사용하고 있지만 실제로 원하는 것은 아닙니다. 문제가 명확하지 않으면 더 많은 코드를 표시 할 수 있습니다. – user1701616

+0

'여백 '이 캔버스에서 작동하지 않습니다. canvas.Left = "30"Canvas.Top = "30"'과 같은 작업을하여 캔버스 내에서 컨트롤의 절대 위치를 설정해야합니다. – mydogisbox