This one took me a bit to figure out.

There were many times where I wanted to bind an element’s property to another’s, but slightly modified. For example:

<Grid x:Name=theGrid>
    <ComboBox x:Name="cbThird"
              Width="{Binding ElementName=theGrid, Path=ActualWidth * 1/3 + 200}"
               />
.
.

The above code snippet is trying to declare that the ComboBox’s width is a third of the grid’s plus an additional 200. Of course, you can’t do this as shown above, and Cider will be quick to catch your foolhardiness. It would appear that your only options would be to do this through the code behind; that would be unfortunate, as you’d have to handle resize events and all that muckery.

Luckily, you can acheive the same effect in pure XAML by making use of Transforms. Below is an example of how you would define the ComboBox’s width to be 1/3 of the grid that it is contained in plus an additional 200.

<Grid x:Name="theGrid">
    <FrameworkElement x:Name="gridTransform" Visibility="Collapsed">
       <FrameworkElement.RenderTransform>
         <TransformGroup x:Name="transformGroup">
          <TranslateTransform X="{Binding ElementName=gridConnect, Path=ActualWidth}"/>
          <ScaleTransform ScaleX="0.33"/>
          <TranslateTransform X="200"/>
         </TransformGroup>
       </FrameworkElement.RenderTransform>
    </FrameworkElement>
    <ComboBox x:Name="cbThird"
        Width="{Binding ElementName=gridTransform, Path=RenderTransform.Value.OffsetX}"
        />

Adjusting the ScaleX attribute will change the width’s multiplicand, while adjusting the X attribute (in the TranslateTransform tag) will adjust the width we’re adding to the result of the multiplication.

Matt Weber

I'm the founder of Bad Echo LLC, which offers consulting services to clients who need an expert in C#, WPF, Outlook, and other advanced .NET related areas. I enjoy well-designed code, independent thought, and the application of rationality in general. You can reach me at matt@badecho.com.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
   
© 2012-2013 Matt Weber. All Rights Reserved. Terms of Use.