Есть в главном окне label
<Label Grid.Row="0" Grid.Column="1" DataContext="{StaticResource h}" Content="{Binding Path=test}"/>
Binding - работает.
Есть в главном окне
<controls:uc x:Name="tttt" DataContext="{StaticResource h}" MyProperty="{Binding Path=test}"/>
Binding - НЕ работает.
Исходник uc
public int MyProperty
{
get { return (int)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); lCaption.Content = value.ToString(); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(int), typeof(uc), new PropertyMetadata(0));
Почему не работает? С label всё отлично но если нужно сделать Binding к label в UserControl начинаются танцы с бубном и всё равно не работает.
DataContext? Он во-первых должен быть не статичным, а во-вторых устанавливаться один раз окну. Далее, как в XAML контрола у вас привязаноMyProperty? – EvgeniyZ Oct 01 '20 at 19:52<UserControl ... x:Name="uc" ...>), далее привяжите свой элемент<Lable Content="{Binding MyProperty, ElementName=uc}">. Можете посмотреть любой ответ тут на SO поUserControl, например вот. – EvgeniyZ Oct 01 '20 at 20:04