wpf - Can we manipulate (subtract) the value of a property while template bidning? -


i defining few grids following:

<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> 

while division works fine , subtraction isn't yielding output.

ialso tried following:

<rowdefinition height="{templatebinding height-(height/5)}"/> 

still no result. suggestions plz.

thanks, subhen

**

update

** in xaml tried implementing ivalueconverter :

<rowdefinition height="{templatebinding height, converter={staticresource heightconverter}}"/> 

added reference

<local:medieelementheight x:key="heightconverter"/> 

in side generic.cs have coded following:

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 exception unknown element height in element rowdefination.

updating code @tony

<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 , implementing xaml file contains follows:

<custommediaelement:customvideocontrol x:name="custmediaelement" width="400" height="300"  nextbtnevent="custmediaelement_nextbtnevent" prevbtnevent="custmediaelement_prevbtnevent" visibility="collapsed"/> 

now, want substract or divide values depending on height of custmediaelement.

you have posed several questions; however, caught in finding solution while overlooking original problem. before attempting answer questions explorer layout expectations.

your provided code suggests have custom control (customvideocontrol) instance height of 300px. controltemplate applied control has 4 rows, have calculated heights based on instance height. based on these settings 4 rows have following values:

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

these total 370px, 70px larger control. means row 3 hidden view, , row 2 clipped down top 40px. assume not intention. if intention, answers below on path. if intention scale row heights based on ratio, can use star sizing. suggested ratio use following settings:

    <grid.rowdefinitions>         <rowdefinition height="240*"/>         <rowdefinition height="20*"/>         <rowdefinition height="60*"/>         <rowdefinition height="50*"/>     </grid.rowdefinitions> 

if still measure height of rows there few corrections need make.

  1. mathematical operations cannot performed within markup extensions (the curly braces). division approach may not throwing xaml parse exception, doubt working correctly. value converter necessary accomplish want.

  2. templatebinding light-weight version of relativesource binding. since templatebinding light-weight doesn't allow converters.

to expected behavior need use binding relativesource. therefore code want looks this:

<rowdefinition height="{binding path=height,                              relativesource={relativesource templatedparent},                              converter={staticresource divisionconverter},                              converterparameter=15}"                        /> 

where divisionconverter key of custom converter. converterparameter in example allows developer pass in denominator, instead of having create separate converter each number.

here's example of custom divisionconverter need create:

public class divisionconverter : ivalueconverter {     #region ivalueconverter members      public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         // default 0. may want handle divide 0          // , other issues differently this.         double result = 0;          // not best code ever, 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 0 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 wanted incorporate both division , subtraction in same binding need either create special converter, or use multibinding (which require create special multibindingconverter).


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -