Can we manipulate (subtract) the value of the property while the template rate?

I am currently defining several grids as follows:

<Grid.RowDefinitions> <RowDefinition Height="{TemplateBinding Height-Height/5}"/> <RowDefinition Height="{TemplateBinding Height/15}"/> <RowDefinition Height="{TemplateBinding Height/20}"/> <RowDefinition Height="{TemplateBinding Height/6}"/> </Grid.RowDefinitions> 

As long as the division works fine, subtraction does not give a result.

Ialso tried the following:

 <RowDefinition Height="{TemplateBinding Height-(Height/5)}"/> 

There is still no result. Any suggestions plz.

Thanks Subhen

**

Update

** Now in my XAML I tried to implement an IvalueConverter, for example:

 <RowDefinition Height="{TemplateBinding Height, Converter={StaticResource heightConverter}}"/> 

Added link as

 <local:medieElementHeight x:Key="heightConverter"/> 

In side generic.cs, I am encoded as follows:

 public class medieElementHeight : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { //customVideoControl objVdeoCntrl=new customVideoControl(); double custoMediaElementHeight = (double)value;//objVdeoCntrl.customMediaPlayer.Height; double mediaElementHeight = custoMediaElementHeight - (custoMediaElementHeight / 5); return mediaElementHeight; } #region IValueConverter Members object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } #endregion } 

But getting the exception Unknown element height in the RowDefination element.

@Tony code update

 <Style TargetType="local:customVideoControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:customVideoControl"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="{TemplateBinding Height-Height/5}"/> <RowDefinition Height="{TemplateBinding Height/15}"/> <RowDefinition Height="{TemplateBinding Height/20}"/> <RowDefinition Height="{TemplateBinding Height/6}"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="2*"/> <ColumnDefinition Width="2*"/> <ColumnDefinition Width="2*"/> </Grid.ColumnDefinitions> <MediaElement x:Name="customMediaPlayer" Source="{TemplateBinding CustomMediaSource}" HorizontalAlignment="Center" VerticalAlignment="Center" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" Grid.Row="0" Grid.ColumnSpan="3" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> 

Now, my XAML executable contains the following:

 <customMediaElement:customVideoControl x:Name="custMediaElement" Width="400" Height="300" nextBtnEvent="custMediaElement_nextBtnEvent" prevBtnEvent="custMediaElement_prevBtnEvent" Visibility="Collapsed"/> 

Now I want to subtract or split the values ​​depending on the height of the custMediaElement.

+6
wpf silverlight xaml
source share
3 answers

You asked some good questions; however, we often find ourselves finding a solution, ignoring the original problem. Before trying to answer your questions, I would like to examine your layout expectations.

The code you provided assumes that you have a custom control (customVideoControl) whose instance is 300 pixels high. The ControlTemplate applied to this control has 4 rows that calculated the height based on the height of the instance. Based on these settings, your 4 lines will have the following meanings:

Line 0: 240 Line 1: 20 Line 2: 60 Line 3: 50

These amounts are 370px, which is 70px more than control. This means that Row 3 will be completely hidden from view, and Row 2 will be cropped to the top 40px. I guess this is not your intention. If this is your intention, then the answers below will hopefully help you along the way. If you intend to scale the height of the line based on the ratio, you can use the size of the star. Your suggested ratio will use the following settings:

  <Grid.RowDefinitions> <RowDefinition Height="240*"/> <RowDefinition Height="20*"/> <RowDefinition Height="60*"/> <RowDefinition Height="50*"/> </Grid.RowDefinitions> 

If you still want to measure the height of the lines, you have to make a few corrections.

  • Mathematical operations cannot be performed in markup extensions (curly braces). Your separation approach cannot throw an xaml syntax exception, but I doubt it works correctly. A value converter is needed to accomplish what you want.

  • TemplateBinding is really just a lite version of RelativeSource binding. Since TemplateBinding is lightweight, it does not allow converters.

To get the expected behavior, you need to use a binding with RelativeSource. So the code you want looks something like this:

 <RowDefinition Height="{Binding Path=Height, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource DivisionConverter}, ConverterParameter=15}" /> 

Where DivisionConverter is the key of the custom converter. The converter in this example allows the developer to pass in the denominator instead of creating a separate converter for each number.

Here is an example of a custom DivisionConverter element that you need to create:

 public class DivisionConverter : IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // Default to 0. You may want to handle divide by zero // and other issues differently than this. double result = 0; // Not the best code ever, but you get the idea. if (value != null && parameter != null) { try { double numerator = (double)value; double denominator = double.Parse(parameter.ToString()); if (denominator != 0) { result = numerator / denominator; } else { // TODO: Handle divide by zero senario. } } catch (Exception e) { // TODO: Handle casting exceptions. } } return result; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion 

If you want to enable both division and subtraction in the same Binding, you will need to either create a special converter or use MultiBinding (which will also require the creation of a special MultiBindingConverter).

+9
source share

I think that for this you need to use the Converter

+2
source share

Instead of subtracting one fifth, why don't you just multiply by .8?

+2
source share

All Articles