Problem to bind custom dependency property? Is it a wpf bug?
It's a fair simple use case:
The main window has two controls: one is a TextBlock and another is a UserControl. I defined a DependencyProperty Text in the UserControl and bind it to the TextBlock's Text property in XAML. But the value is not shown up.
UserControl1.CS:
public partial class UserControl1 : System.Windows.Controls.UserControl
{
public UserControl1()
{
InitializeComponent();
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(UserControl1), new PropertyMetadata());
public string Text
{
set
{
SetValue(TextProperty, value);
this.label.Text = value;
}
get
{
return (string)GetValue(TextProperty);
}
}
}
UserControl XAML:<TextBlock Name="label">bcd</TextBlock>
main Window XAML:
<StackPanel>
<TextBlock Name="tb">abc</TextBlock>
<l:UserControl1 Text="{Binding ElementName=tb, Path=Text}"/>
</StackPanel>
[1245 byte] By [
lzy_tek] at [2008-1-8]
So I've come across a different problem related to this example. If you make the TextBlocks TextBoxes, everything works fine UNTIL you change the value in the TextBox that is in the User Control. Once you do this, it seems as though the binding is gone. To test this, just change the text in the UC's TextBox, then change the value in the original TextBox. The UC's TextBox Text will not change. Is this a bug? Or does there have to be some special binding property set?
I tried to simplify my problem too much - that works as long as you set the binding Mode=TwoWay. My particular example is with setting the DataContext. I have a user control that acts as a selection of a child object. The page has a data context which is an object - lets say a Person. I set my user control's data context to Person.Company. My user control will change the Data Context it is bound to - basically change to a different Company. And the TextBox in the user control is set to the Text DP of the User Control - which is bound to Company.Name. Here's some code...
User Control:
Code Snippet
<
UserControl x:Class="MyTestInfragisticsXBAP.Lookup" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MyTest" x:Name="root" >
<
Grid> <
Grid.ColumnDefinitions> <
ColumnDefinition Width="50"/> <
ColumnDefinition Width="Auto"/> </
Grid.ColumnDefinitions> <
TextBox Name="LookupTextBox" Text="{Binding Text, ElementName=root}"></TextBox> <
Button Grid.Column="1" Click="ChangeSource">Lookup</Button> </
Grid> </
UserControl> The code behind just defines the Text DependencyProperty and the ChangeSource just creates a new Company and does this.DataContext = newCompany;
The Page just has the userControl (and some other controls to verify things work, but it's mainly this:
<
local:Lookup Grid.Column="1" Grid.Row="2" DataContext="{Binding Path=Editors, Mode=TwoWay}" Text="{Binding Name}"/> I set the DataContext of the page in the code behind. As I said in my earlier post, everything works except when you modify the value in the UC's TextBox. Then the binding gets lost. I think I've tried every possible combination of binding Modes and that didn't work. I tried ClearValue of the Text DP and that literally sets the value to a blank string and it never regains the binding value.