Tworzę prosty User Control w WPF, który zawiera TextBlock wewnątrz przycisku.UserControl Dependency Property czas projektowania
<UserControl x:Class="WpfExpansion.MyButton"..... >
<Grid >
<Button Background="Transparent" >
<TextBlock Text="{Binding Path=Text}"/>
</Button>
</Grid>
</UserControl>
A także właściwość zależności "Tekst".
public partial class MyButton : UserControl
{
public MyButton()
{
InitializeComponent();
this.DataContext = this;
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MyButton), new PropertyMetadata(string.Empty));
}
A potem używam UserControl tak:
<MyButton Text="Test" />
Problem polega na tym, że Visual Studio projektowe nie zmienia, ale to działa w czasie wykonywania.
Co jest nie tak?
Próbowałem też
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Wewnątrz definicji UC, bez powodzenia.
Great! W pierwszej chwili nie wydaje się to działać, więc zamykam i otwieram Visual Studio, a teraz działa dobrze. Wielkie dzięki. – Guilherme