2012-10-18 44 views
2

让我的用户控件绑定到列表<>我有点麻烦。它的伟大工程,当我试图做这样的事情:将列表绑定到UserControl属性

public string Map 
{ 
    get { return (string)GetValue(MapProperty); } 
    set 
    { 
     SetValue(MapProperty, value); 
    } 
} 

public static readonly DependencyProperty MapProperty = 
DependencyProperty.Register(
    "Map", 
    typeof(string), 
    typeof(GamePane), 
    new PropertyMetadata(  
     "Unknown",    
     ChangeMap) 
    ); 

但是,如果我尝试使用属性,它是什么,更多的则是字符串,整数或浮点数等我得到的”成员‘属性名称’不是认可或不可访问“。例如:

public List<string> Players 
{ 
    get { return (List<string>)GetValue(PlayersProperty); } 
    set 
    { 
     SetValue(PlayersProperty, value); 
    } 
} 

public static readonly DependencyProperty PlayersProperty = 
DependencyProperty.Register(
    "Players", 
    typeof(List<string>), 
    typeof(GamePane), 
    new PropertyMetadata(  
     new List<string>(), 
     ChangePlayers) 
    ); 

除了类型代码是完全一样的。

我已经看到,我可能需要使用BindableList,但是,这似乎不存在Windows 8项目。

有人能指出我在正确的方向或给我看另一种方法。

编辑:按要求,XAML为我的列表视图,这是我尝试绑定字符串列表:

<ListView x:Name="PlayerList" SelectionMode="None" ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.VerticalScrollMode="Disabled" ItemsSource="{Binding Players}" 
        ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" Margin="6,-1,0,0" IsHitTestVisible="False"> 

然后,在我的主要看法,我画我的GridView,其创建我的绑定和有例外:

<GridView 
    x:Name="currentGames" 
    AutomationProperties.AutomationId="ItemsGridView" 
    AutomationProperties.Name="Items" 
    TabIndex="1" 
    Padding="12,0,12,0" 
    ItemsSource="{Binding Source={StaticResource itemsViewSource}}" 
    SelectionMode="None" 
    IsSwipeEnabled="false" Grid.Row="1" Margin="48,-20,0,0" Height="210" VerticalAlignment="Top" > 
    <GridView.ItemTemplate> 
     <DataTemplate> 
      <local:GamePane Map="{Binding Map}" Time="{Binding TimeRemaining}" Players="{Binding Players}"/> 
     </DataTemplate> 
    </GridView.ItemTemplate> 
</GridView> 

有趣的是,这个XAML打破两个Visual Studio的与交融的设计,代码将被执行。虽然,我的玩家不会出现。

+0

你也可以张贴在您尝试绑定到该属性的XAML? – nemesv

+0

我已经从我的MainPage和UserControl中发布了处理绑定的XAML,我遇到了麻烦。 – Runewake2

回答

1

是的,它的工作原理。

这里是绑定到它的XAML:

<Grid Background="Black"> 
    <local:MyUserControl x:Name="MyControl" /> 
    <ListBox ItemsSource="{Binding MyList, ElementName=MyControl}" /> 
</Grid> 

而这里的用户控件代码:

public sealed partial class MyUserControl : UserControl 
{ 
    public MyUserControl() 
    { 
     this.InitializeComponent(); 
    } 

    public string[] MyList 
    { 
     get { return new string[] { "One", "Two", "Three" }; } 
    } 
} 
+0

这就像一个魅力。谢谢一堆! – Runewake2

相关问题