I have the following scenario:
public class HubModel
{
public string Name { get; set; }
}
I create an ObservableCollection in my ViewModel and set the DataContext on the HubPage to that ViewModel.
On my HubPage I have a simple UserControl called TestUserControl.
XAML from the UserControl:
<UserControl
x:Name="userControl"
....>
<Grid>
<StackPanel Orientation="Vertical">
<ItemsControl x:Name="ItemsContainer" ItemsSource="{Binding Collection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Margin="0,0,0,20">
<StackPanel>
<TextBlock Foreground="Black" HorizontalAlignment="Left" FontFamily="Arial" FontSize="42" VerticalAlignment="Center" Name="CurrencyTextBlock" Text="{Binding Path=Text,ElementName=userControl}"></TextBlock>
</StackPanel>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
</UserControl>
UserControl code behind:
public ObservableCollection<object> Collection
{
get { return (ObservableCollection<object>)GetValue(CollectionProperty); }
set { SetValue(CollectionProperty, value); }
}
public static readonly DependencyProperty CollectionProperty =
DependencyProperty.Register("Collection", typeof(ObservableCollection<object>), typeof(TestUserControl), new PropertyMetadata(null));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(TestUserControl), new PropertyMetadata(string.Empty));
Because my UserControl should not know HubModel I wanna bind the TextBlock Text-Path via DependencyProperty.
XAML from the HubPage:
...
<userControls:TestUserControl Collection="{Binding TestCollection}" Text="Name"/>
...
Collection="{Binding TestCollection}" sets the list to the DependencyProperty in my UserControl.
Text="Name" sets the property name. The plan is that my UserControl finds for the TextBlock Text "Name" in the DependencyProperty and takes the value from the Property "Name" from the binded class HubModel.
The problem is that my UserControl finds "Name" in the DependencyProperty and shows "Name" for every entry in the collection instead of the property value in the class.
Is something like this possible? Or what's the best way for binding within UserControls. In my optionen the should not know the property name from the binded classes.
Thanks Daniel