Reputation: 746
I am trying to create a user control that has a contentcontrol which binds its content tio a dependancy object. The code is as follows
Xaml
<ContentControl Grid.Column="1" HorizontalAlignment="Stretch" x:Name="NotifyContent" Content="{Binding ElementName=notifyBarCtrl, Path=Content, NotifyOnSourceUpdated=True}" />
c#
public object Content
{
get { return (object)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
// Using a DependencyProperty as the backing store for Content. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register("Content", typeof(object), typeof(NotifyBar));
The problem I'm having is that when I try to define the content as a textblock using xaml and then bind the text property to a string in my application it fails to update when the string changes:
My application code is as follows
Xaml
<ctrl:NotifyBar DockPanel.Dock="Top" Background="White" HorizontalAlignment="Stretch" >
<ctrl:NotifyBar.Content>
<TextBlock Height="30" Text="{Binding ElementName=MainWindow, Path=NotifyMessage, Mode=TwoWay}" HorizontalAlignment="Stretch" />
</ctrl:NotifyBar.Content>
</ctrl:NotifyBar>
c#
public string NotifyMessage
{
get { return _NotifyMessage; }
set
{
if (_NotifyMessage != value)
{
_NotifyMessage = value;
OnPropertyChanged("NotifyMessage");
}
}
}
any suggestions as to what I might be missing or might have done wrong would be much appreciated.
Thanks
[Edit]
I have since changed the Content dependancy property to NotifyContent as I was getting a warning that it was overriding the Content to UserControl but this still hasnt resolved the issue
Upvotes: 1
Views: 831
Reputation: 746
So I figured out that it was because I was using elementname within the the textblock.
If I set the DataContext of the usercontrol to the element and bind to the propertiesit works.
see below:
<ctrl:NotifyBar DataContext="{Binding ElementName=MainWindow}" DockPanel.Dock="Top" Background="White" HorizontalAlignment="Stretch" >
<ctrl:NotifyBar.Content>
<TextBlock Height="30" Text="{Binding Path=NotifyMessage, Mode=TwoWay}" HorizontalAlignment="Stretch" />
</ctrl:NotifyBar.Content>
</ctrl:NotifyBar>
Upvotes: 0