471,337 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

DataBinding through aggregation

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>
================================================== =
Jun 27 '08 #1
1 1096
"Vivien Parlat" <po****@gmail.comwrote in message
news:3b**********************************@j22g2000 hsf.googlegroups.com...
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

Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Nice Chap | last post: by
5 posts views Thread by John Wood | last post: by
2 posts views Thread by Jozsef Bekes | last post: by
4 posts views Thread by Frederik Vanderhaegen | last post: by
23 posts views Thread by SenthilVel | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.