2012-11-19 38 views
0

我定義自己的控制 - NameInfoControl,這是基於UserControl徹底XAML:如何通過XAML設置`myListView`的值?

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

它看起來如此:

enter image description here

我的控制已經ListView項目,命名爲 myListView。如何通過XAML爲實例設置ItemsSource屬性 myListView的值?我需要它在下面的代碼:

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

回答

1

在你的NameInfoControl綁定的XAML你myListView的項目控件的DataContext

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

然後在你使用父XAML該控件綁定到DataContext包含應顯示的項目清單:

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

+0

+1只需在最後一行中將Binging更改爲BinDing即可。 – Blachshma

+0

但是,我必須從'ListBox.SelectedItem.Items'獲取項目源,它們位於'MainWindow'上。我的'NameInfoControl'實例也位於這個窗口上,bot作爲嵌套在其他控件中。我試過這樣的代碼:'DataContext =「{Binding ElementName = lstAcads,Path = SelectedItem}」ItemsSource =「{Binding Path = HKLMApplications}」'但它不適用於我。 –

+0

將一個新屬性添加到包含SelectedItems的主窗口中並與之綁定。看起來你已經到了一個複雜的地步,你應該開始考慮使用MVVM模式。 – bitbonk