1

XAML は初めてです。ItemsControl について検索したところ、わかりやすいチュートリアルが見つかりましたが、WinRT で動作しないことが問題です。

チュートリアル: https://rachel53461.wordpress.com/2011/09/17/wpf-itemscontrol-example/

タグで使用しようとしTargetTypeましたが、実行時に例外が発生しました。Style

<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>
4

1 に答える 1

3

あなたの問題はここにあります:

<Style TargetType="TextBox">

これはあるべきものです:

<Style TargetType="ContentPresenter">

ItemContainerItemsControlですContentPresenter(特定のアイテムが に追加されない限りItemsControl)。

したがって、ビュー階層は次のようになります ( をItemsPanel以外のものに変更していないと仮定しますStackPanel):

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

編集:

Scott がコメントで指摘したように、このソリューションは実際には 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 });
        }
    }
}

これにより、 がコレクション内の各アイテムCanvas.LeftPropertyのプロパティにバインドされ、同様にとプロパティがバインドされます。XCanvas.TopPropertyY

于 2012-09-27T03:09:00.063 に答える