0

ItemsPanelTemplateの概念を理解しようとしています。このために、私は小さなサンプルソリューションを構築しました。

次のコードを持つUserControl「MyListView」があります。

MyListView.xaml:

<UserControl x:Class="WpfApplication2.MyListView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <Style  TargetType="ListViewItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Border BorderBrush="Black" BorderThickness="1" Margin="0" Padding="0" Width="100" Background="Gray">
                            <TextBlock Text="Text" HorizontalAlignment="Center"  />
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<ListView ItemsSource="{Binding Path=TreeItemChildren}">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">

            </StackPanel>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>
</UserControl>

MyListView.csに、表示されるデータをバインドするためのDependencyPropertyを追加しました。

public partial class MyListView : UserControl
{
    public MyListView()
    {
        this.TreeItemChildren = new ObservableCollection<string>();
        this.TreeItemChildren.Add("Text0");
        this.TreeItemChildren.Add("Text1");
        this.TreeItemChildren.Add("Text2");

        InitializeComponent();
    }

    public ObservableCollection<string> TreeItemChildren
    {
        get { return (ObservableCollection<string>)GetValue(TreeItemChildrenProperty); }
        set { SetValue(TreeItemChildrenProperty, value); }
    }

    // Using a DependencyProperty as the backing store for TreeItemChildren.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TreeItemChildrenProperty =
        DependencyProperty.Register("TreeItemChildren", typeof(ObservableCollection<string>), typeof(MainWindow), new UIPropertyMetadata(null));
}

メインウィンドウでこのUserControlを使用しようとすると、表示されるデータがありません。その理由は何ですか?

4

1 に答える 1

0

コントロールテンプレートにプロパティをバインドしていません。そのため、は表示されません。ListBoxItemContent

通常、これを置き換えます。

<TextBlock Text="Text" HorizontalAlignment="Center"  />

と:

<ContentPresenter HorizontalAlignment="Center"/>

(テンプレート化されたコントロールがaContentControlの場合、関連するプロパティに自動的ContentPresenterにバインドされます)Content

また、ListView.ItemsSourceのプロパティへのバインドDataContext(設定されていないため、コントロールのインスタンスのバインドに干渉するため、設定しないでください)、何らかの方法でターゲットに変更しUserControlます(例:useElementNameまたはRelativeSource)。

(バインディングによって引き起こされるバインディングエラーがあるはずです。それらをデバッグする方法をItemsSource学びましょう)

于 2012-01-13T18:03:23.473 に答える