Reputation: 481
I'm having a bit of trouble getting my User Control to bind to a List<>. It works great when I attempt to do something like:
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)
);
However, if I attempt to use a Property that is anything more then a string, int or float etc I get "the member 'Property Name' is not recognized or is not accessible". Example:
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)
);
Besides the types the code is exactly the same.
I have seen that I might need to use a BindableList, however, this does not appear to exist for Windows 8 projects.
Can someone point me in the proper direction or show me an alternative approach.
Edit: By request, the XAML for my List View, which I attempt to bind the string list to:
<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">
Then, in my main view, I draw my GridView, which create my Bindings and has the exception:
<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>
Interestingly, while this XAML breaks both Visual Studio's and Blend's designer, the code will execute. Albeit, my Players will not show up.
Upvotes: 3
Views: 3121
Reputation: 31831
Yeah, it works.
Here's the XAML to bind to it:
<Grid Background="Black">
<local:MyUserControl x:Name="MyControl" />
<ListBox ItemsSource="{Binding MyList, ElementName=MyControl}" />
</Grid>
And here's the user control code:
public sealed partial class MyUserControl : UserControl
{
public MyUserControl()
{
this.InitializeComponent();
}
public string[] MyList
{
get { return new string[] { "One", "Two", "Three" }; }
}
}
Upvotes: 2