Chci si vytvořit vlastní UserControl, který bude obsahovat několik DependencyProperties. Na ty se pak přes Binding navážu z XAML.
Primitivní:
- Kód: Vybrat vše
public int Dummy
{
get { return (int)GetValue(DummyProperty); }
set { SetValue(DummyProperty, value); }
}
// Using a DependencyProperty as the backing store for Dummy. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DummyProperty =
DependencyProperty.Register("Dummy", typeof(int), typeof(UserControl1), new UIPropertyMetadata(10));
XAML
- Kód: Vybrat vše
<UserControl x:Class="WpfApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource self}}">
<Grid>
<TextBlock Text="{Binding Dummy}" />
</Grid>
</UserControl>
a v MainWindow:
- Kód: Vybrat vše
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<src:UserControl1 Dummy="20" />
</StackPanel>
</Window>
Po spuštění aplikace vše chodí jak má - zobrazí se TextBlock s "20". V designtime ve Visual Studiu (2010, .NET4) se ale "20" nezobrazí - a to ani v náhledu v MainWindow ani v náhledu v UserControl1.
Co máme udělat pro to, abych ihned viděl datové vazby ve VS - jak přímo v UserControl, tak poté na místech, kde ji používám?