Reputation: 1358
I'm new to WPF and I'm try to bind a dependacy property. I want that the the text I write on the WPFCtrl:FilterTextBox will be displaied in the TextBlock
here is my XAML
xmlns:WPFCtrl="clr-namespace:WPFControls"
xmlns:local="clr-namespace:WpfApplication9"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:Person x:Key="myDataSource" />
</Window.Resources>
<Grid>
<StackPanel>
<StackPanel.DataContext>
<Binding Source="{StaticResource myDataSource}"/>
</StackPanel.DataContext>
<WPFCtrl:FilterTextBox Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged }"/>
<TextBlock Width="55" Height="25" Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</Grid>
here the Person Class
namespace WpfApplication9
{
public class Person : INotifyPropertyChanged
{
private string name = "";
// Declare the event
public event PropertyChangedEventHandler PropertyChanged;
public Person()
{
}
public Person(string value)
{
this.name = value;
}
public string Name
{
get { return name; }
set
{
name = value;
// Call OnPropertyChanged whenever the property is updated
OnPropertyChanged("Name");
}
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
}
and the FilterTextBox Text Property
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(FilterTextBox), new PropertyMetadata());
public string Text
{
//get { return _tbFilterTextBox.Text == null ? null : _tbFilterTextBox.Text.TrimEnd(); }
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
//set { _tbFilterTextBox.Text = value; }
}
The problem is that it does not enter in OnPropertyChanged() What am I doing wrong?
Upvotes: 0
Views: 1036
Reputation: 84684
The problem is that the TextProperty
in FilterTextBox
doesn't bind TwoWay
by default.
Either set the BindingMode
to TwoWay
<WPFCtrl:FilterTextBox Text="{Binding Path=Name,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged }"/>
Or change the metadata for the DependencyProperty
Text
so it binds twoway by default
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text",
typeof(string),
typeof(FilterTextBox),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
Upvotes: 1
Reputation: 9891
does this "FilterTextBox" Control update the DP everytime text is inserted?
i guess the FilterTextBox has a ControlTemplate with a regular TextBox inside. something like
<ControlTemplate TargetType="{x:Type FilterTextBox}">
<TextBox Name="PART_FilterTextBoxInputField" Text="{TemplateBinding Text}"/>
</ControlTemplate>
you need to set the binding where the internal textbox binds to your Text-Dependcy Property to use UpdateSourceTrigger=PropertyChanged too. otherwise the binding will only update when the textbox loses focus.
Upvotes: 1