0

我定义了我自己的控件 - NameInfoControl,它基于UserControl彻底的 XAML:

<UserControl x:Class="AcadLoadManager.NameInfoControl"
             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">
    <Grid>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <ToolBar>
                <Button x:Name="btnAdd" x:FieldModifier="public" ToolTip="Add record" >
                    <Image Source="/AcadLoadManager;component/Icons/bullet_sparkle.png" Width="16"/>
                </Button>
                <Button x:Name="btnEdit" x:FieldModifier="public" ToolTip="Edit record">
                    <Image Source="/AcadLoadManager;component/Icons/bullet_edit.png" Width="16"/>
                </Button>
                <Button  x:Name="btnRemove" x:FieldModifier="public" ToolTip="Remove record">
                    <Image Source="/AcadLoadManager;component/Icons/bullet_cross.png" Width="16"/>
                </Button>
            </ToolBar>
            <ListView x:Name="myListView" x:FieldModifier="public" Margin="3" Grid.Row="1">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Width="100" Header="Global name" 
                                        DisplayMemberBinding="{Binding GlobalName}"/>
                        <GridViewColumn Width="100" Header="Local name" 
                                        DisplayMemberBinding="{Binding LocalName}"/>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    </Grid>
</UserControl>

看起来是这样的:

在此处输入图像描述

我的控件有ListView一个名为myListView的项目。例如,如何通过 XAML设置myListViewItemsSource的属性值?我在下一个代码中需要它:NameInfoControl

<GroupBox Header="Command groups:" Grid.Column="0" Grid.Row="1" Margin="5">
    <local:NameInfoControl/>
</GroupBox>
4

1 回答 1

1

在您的 XAML 中,将您NameInfoControl的项目绑定myListViewDataContext您的控件:

<ListView ItemsSource="{Binding}" x:Name="myListView" x:FieldModifier="public" Margin="3" Grid.Row="1">

然后在使用该控件的父 XAML 中将 绑定DataContext到包含应显示的项目的列表:

<local:NameInfoControl DataContext="{Binding MyCollectionOfItems}" />

于 2012-11-19T12:02:58.673 回答