Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - XAML binding between controls?

 
 
Old 04-17-2006   #1 (permalink)
CosminB [BRT]


 
 

XAML binding between controls?

Is it possible to bind between two controls? For example to be able to
do something like:

1. Have a button alter its width according to the current value of a
slider:

<Slider x:Name="MySlider" Minimum="10" Maximum="200" Value="120">
</Slider>

<Button Width="{Binding MySlider.Value}">
Click Me!
</Button>

or

2. Make a rectangle always have complete round left-right sides, no
matter its size:

<Rectangle Width="200" Height="50" RadiusX="{Binding Height/2}"
RadiusY="{Binding Height/2}">
</Rectangle>

Is this possible within XAML, or you just have to go code-behind too to
achieve this?

Thanks,
Cosmin.


My System SpecsSystem Spec
Old 04-17-2006   #2 (permalink)
Douglas Stockwell


 
 

Re: XAML binding between controls?

1. Easy:

<Button Width="{Binding ElementName=MySlider, Path=Value}" ...

2.

I think the best way to do this is to write a converter - see the documentation
for System.Windows.Data.Binding and System.Windows.Data.IValueConverter.

- Doug

> Is it possible to bind between two controls? For example to be able to
> do something like:
>
> 1. Have a button alter its width according to the current value of a
> slider:
>
> <Slider x:Name="MySlider" Minimum="10" Maximum="200" Value="120">
> </Slider>
>
> <Button Width="{Binding MySlider.Value}">
> Click Me!
> </Button>
> or
>
> 2. Make a rectangle always have complete round left-right sides, no
> matter its size:
>
> <Rectangle Width="200" Height="50" RadiusX="{Binding Height/2}"
> RadiusY="{Binding Height/2}">
> </Rectangle>
> Is this possible within XAML, or you just have to go code-behind too
> to achieve this?
>
> Thanks,
> Cosmin.



My System SpecsSystem Spec
Old 04-17-2006   #3 (permalink)
CosminB [BRT]


 
 

Re: XAML binding between controls?

> 1. Easy:
>
> <Button Width="{Binding ElementName=MySlider, Path=Value}" ...


Thanks, worked!

> 2.
>
> I think the best way to do this is to write a converter - see the documentation
> for System.Windows.Data.Binding and System.Windows.Data.IValueConverter.


I was thinking of implementing something like MultiplyConverter
together with MultiBinding, so what I would actually need is to pass
the height as the first parameter, and the value 0.5 as the second
parameter. This would actually give me half the height. But I don't
know how to do that, I'm having problems both in code and in markup.
Here's what I have so far:

--- XAML ---

<Rectangle
Grid.Column="0" Grid.Row="0" Grid.RowSpan="2"
Fill="#4171A6">
<Rectangle.RadiusY>
<MultiBinding Converter="{StaticResource conv}">
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource self}" Path="Height"
/>
<Binding Source="0.5" />
</MultiBinding.Bindings>
</MultiBinding>
</Rectangle.RadiusY>
<Rectangle.RadiusX>
<Binding RelativeSource="{RelativeSource self}" Path="RadiusY" />
</Rectangle.RadiusX>
</Rectangle>

However, with this markup, I get the following in the objects[] array
of the converter:
NaN, "0.5"; so I get an invalid number and a string... how can I obtain
doubles?

My System SpecsSystem Spec
Old 04-17-2006   #4 (permalink)
Drew Marsh


 
 

Re: XAML binding between controls?

CosminB wrote:

> I was thinking of implementing something like MultiplyConverter
> together with MultiBinding, so what I would actually need is to pass
> the height as the first parameter, and the value 0.5 as the second
> parameter. This would actually give me half the height. But I don't
> know how to do that, I'm having problems both in code and in markup.


You don't need to use a MultiBinding. You can pass a parameter to your converter
via Binding's aptly named ConverterParameter property[1].

HTH,
Drew

[1] http://windowssdk.msdn.microsoft.com...rParameter.asp


My System SpecsSystem Spec
Old 04-17-2006   #5 (permalink)
CosminB [BRT]


 
 

Re: XAML binding between controls?

Yep, that's an option too.
Is there a way to have my "0.5" value be passed as a double? Receiving
a string kindof messes code up.

My System SpecsSystem Spec
Old 04-17-2006   #6 (permalink)
Drew Marsh


 
 

Re: XAML binding between controls?

CosminB wrote:

> Yep, that's an option too.
> Is there a way to have my "0.5" value be passed as a double? Receiving
> a string kindof messes code up.


Yep. I don't have time to check this code for you, but it should work. Basically
you need to use the expanded syntax for binding like so:

<!-- first, need to map BCL System namespace -->
<Window xmlns:system="clr-namespace:System;assembly=mscorlib" ....>

.....

<Rectangle.RadiusY>
<Binding RelativeSource="{RelativeSource self}" Path="Height">
<Binding.ConverterParameter>
<!-- use a Double from System -->
<systemouble>0.5</systemouble>
</Binding.ConverterParameter>
</Binding>
</Rectangle.RadiusY>

HTH,
Drew

___________________________________
Drew Marsh
Chief Software Architect
Mimeo.com, Inc. - http://www.mimeo.com
Microsoft C# / WPF MVP
Weblog - http://blog.hackedbrain.com/



My System SpecsSystem Spec
Old 04-17-2006   #7 (permalink)
CosminB [BRT]


 
 

Re: XAML binding between controls?

Thanks, it worked.

I hope MS will put more effort into this area, it's hard to get things
done using this much markup and codebehind also. What I am trying to do
now is design a button template, I didn't know I would have to add code
behind only for this.

Also, I thought I could use a nested Bindings and MultiBindings, but
this isn't possible. I have designed two converters for multiplication
and addition. If nesting was possible, then it could have been possible
to calculate ((A+B)*C). But I only can A*B and A+B.

As it stands, XAML is quite poor when it comes to finetuning its
controls, I thought they did a good job, I am dissapointed. Or maybe
it's just that it's not enough documentation available, who knows...

My System SpecsSystem Spec
Old 04-17-2006   #8 (permalink)
Douglas Stockwell


 
 

Re: XAML binding between controls?

Is this really messy?

return Convert.ToDouble(value) * Convert.ToDouble(parameter)

- Doug

> Yep, that's an option too.
> Is there a way to have my "0.5" value be passed as a double? Receiving
> a string kindof messes code up.



My System SpecsSystem Spec
Old 04-18-2006   #9 (permalink)
Ranj


 
 

Re: XAML binding between controls?

I am finding things that should be simply requiring more work and at the
moment its less intuitive, but I have used OpenLaszlo in the past its much
seems simpler to do some things and it can be doen in the markup

Have a look at the object orientation samples at

http://www.laszlosystems.com/lps/laszlo-in-ten-minutes/

Ok OpenLaszlo doesn't do all the things that Avalon does do, but I find it
much simpler and intuitive and can be done in the markup ..see below:

<canvas>

<simplelayout spacing="5"/>

<class name="box" height="100" width="100" bgcolor="red"/>

<class name="borderedbox" extends="box">
<attribute name="bordersize" value="3"/>
<view bgcolor="yellow"
x="${parent.bordersize}"
y="${parent.bordersize}"
width="${parent.width - parent.bordersize*2}"
height="${parent.height - parent.bordersize*2}"/>
</class>

<borderedbox/>
<borderedbox bordersize="6"/>
<borderedbox bordersize="9"/>

</canvas>


Regards
Ranj

"Douglas Stockwell" <doug@remove.11011.net> wrote in message
news:eca9ec38130b0e8c830e84bf3aae0@news.microsoft.com...
> Is this really messy?
>
> return Convert.ToDouble(value) * Convert.ToDouble(parameter)
>
> - Doug
>
>> Yep, that's an option too.
>> Is there a way to have my "0.5" value be passed as a double? Receiving
>> a string kindof messes code up.

>
>



My System SpecsSystem Spec
Old 04-18-2006   #10 (permalink)
Douglas Stockwell


 
 

Re: XAML binding between controls?

I agree that these binding expressions would be useful. I wanted a feature
like this a some time ago, but I've come to live without it.

There could be many reasons why this isn't in WPF but a few that I can think
of would be:
- XAML/WPF is language neutral - how to choose / design an expression language?
- XAML is for tools: back in the time of Mini Languages (http://blogs.msdn.com/johngossman/ar...08/478593.aspx)
this may have been more likely

That said, I think it would be pretty simple to build an expression evaluator
(as a converter) for a MultiBinding. Unfortunately the markup wouldn't be
quite so elegant.

Something like:

<Border>
<Rectangle>
<Rectangle.Width>
<MultiBinding Converter="{StaticResource ExpressionConverter}" ConverterParameter="{0}
- {1} * 2">
<MultiBinding.Bindings>
<Binding Path="Parent.Width" />
<Binding Path="Parent.BorderThickness" />
</MultiBinding.Bindings>
</MultiBinding>
</Rectangle.Width>
</Rectangle>
</Border>

- Doug

> I am finding things that should be simply requiring more work and at
> the moment its less intuitive, but I have used OpenLaszlo in the past
> its much seems simpler to do some things and it can be doen in the
> markup
>
> Have a look at the object orientation samples at
>
> http://www.laszlosystems.com/lps/laszlo-in-ten-minutes/
>
> Ok OpenLaszlo doesn't do all the things that Avalon does do, but I
> find it much simpler and intuitive and can be done in the markup ..see
> below:
>
> <canvas>
>
> <simplelayout spacing="5"/>
>
> <class name="box" height="100" width="100" bgcolor="red"/>
>
> <class name="borderedbox" extends="box">
> <attribute name="bordersize" value="3"/>
> <view bgcolor="yellow"
> x="${parent.bordersize}"
> y="${parent.bordersize}"
> width="${parent.width - parent.bordersize*2}"
> height="${parent.height - parent.bordersize*2}"/>
> </class>
> <borderedbox/>
> <borderedbox bordersize="6"/>
> <borderedbox bordersize="9"/>
> </canvas>
>
> Regards
> Ranj
> "Douglas Stockwell" <doug@remove.11011.net> wrote in message
> news:eca9ec38130b0e8c830e84bf3aae0@news.microsoft.com...
>
>> Is this really messy?
>>
>> return Convert.ToDouble(value) * Convert.ToDouble(parameter)
>>
>> - Doug
>>
>>> Yep, that's an option too.
>>> Is there a way to have my "0.5" value be passed as a double?
>>> Receiving
>>> a string kindof messes code up.



My System SpecsSystem Spec
 

Thread Tools



Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46