DataBinding through aggregation

V

Vivien Parlat

Hello,

Here is the description of the code I have:

- a class myClass, inheriting from DependencyObject, containing a
DependencyProperty myString (yes, my names are ugly)
- a class toStringConverter implementing IValueConverter, which takes
a myClass and returns its myString
- the following XAML code (see below).

When I click on the top button, only the first string is modified,
because the second textblock is bound not to the property, but to the
object.

My question itself is simple: how can I automatically update the
second textblock when myString property is modified ? I only need such
a binding in OneWay mode, so how can I "notify" wpf engine to reflect
any modification of the myString value like if it was a modification
of the container itself ? (keeping the same syntax; I simplified the
example so I don't have the flexibility to do it the first textblock's
way).

Thanks in advance

============= the xaml code ===============================

<Window x:Class="WpfApplication6.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfApplication6"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<my:myClass x:Key="maClasse" myString="AbC" />
<my:toStringConverter x:Key="ToStCo" />
</Window.Resources>
<Grid DataContext="{StaticResource maClasse}">
<StackPanel Orientation="Vertical">
<Button Click="Button_Click">Go</Button>
<TextBlock Text="{Binding myString}"/>
<TextBlock>
<TextBlock.Text>
<Binding Converter="{StaticResource ToStCo}" />
</TextBlock.Text>
</TextBlock>
</StackPanel>
</Grid>
</Window>
===================================================
 
C

Chris Jobson

Vivien Parlat said:
Hello,

Here is the description of the code I have:

- a class myClass, inheriting from DependencyObject, containing a
DependencyProperty myString (yes, my names are ugly)
- a class toStringConverter implementing IValueConverter, which takes
a myClass and returns its myString
- the following XAML code (see below).

When I click on the top button, only the first string is modified,
because the second textblock is bound not to the property, but to the
object.

My question itself is simple: how can I automatically update the
second textblock when myString property is modified ? I only need such
a binding in OneWay mode, so how can I "notify" wpf engine to reflect
any modification of the myString value like if it was a modification
of the container itself ? (keeping the same syntax; I simplified the
example so I don't have the flexibility to do it the first textblock's
way).

Thanks in advance

============= the xaml code ===============================

<Window x:Class="WpfApplication6.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfApplication6"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<my:myClass x:Key="maClasse" myString="AbC" />
<my:toStringConverter x:Key="ToStCo" />
</Window.Resources>
<Grid DataContext="{StaticResource maClasse}">
<StackPanel Orientation="Vertical">
<Button Click="Button_Click">Go</Button>
<TextBlock Text="{Binding myString}"/>
<TextBlock>
<TextBlock.Text>
<Binding Converter="{StaticResource ToStCo}" />
</TextBlock.Text>
</TextBlock>
</StackPanel>
</Grid>
</Window>
===================================================

I'm certainly no expert on WPF, but as far as I know there's no simple way
to do what you want. I can think of one rather nasty way round which I think
will work, but I don't recommend it!

1. Implement INotifyPropertyChanged in myClass.
2. Define another property on myClass which just returns "this", e.g.:
public MyClass Me { get { return this; } }
3. In the PropertyChangedCallback for the myString dependency property fire
a change notification for the new property:
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Me"));
4. Change the XAML to bind to the new property instead of to the object::
<TextBlock Text="{Binding Me, Converter={StaticResource ToStCo}}" />

Chris Jobson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top