![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | 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. |
| | #2 (permalink) |
| Guest | 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. |
| | #3 (permalink) |
| Guest | 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? |
| | #4 (permalink) |
| Guest | 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 |
| | #6 (permalink) |
| Guest | 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 --> <system ouble>0.5</system ouble></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/ |
| | #7 (permalink) |
| Guest | 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... |
| | #9 (permalink) |
| Guest | 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. > > |
| | #10 (permalink) |
| Guest | 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. |
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| XAML and binding problem | Lloyd Dupont | Avalon | 5 | 06-15-2007 08:43 AM |
| WPF user controls without XAML | Cairn | Avalon | 2 | 04-12-2007 01:36 PM |
| XAML Binding / Animated Expander | gregbacchus@hotmail.com | Avalon | 1 | 03-14-2007 04:57 AM |
| XAML and binding to datatables (ADO.net) | Magne | Avalon | 4 | 01-03-2007 11:10 PM |
| Loading a XAML File Containing Controls | Bree | Avalon | 2 | 12-07-2006 08:55 AM |