none
Subtract a fixed value from a property

    Question

  • Hi,

     

    how can i subtract a fixed value from a width property in a datatemplate?

     

    Here is the DataTemplate :

    Code Snippet

    <Style x:Key="gstListBox" TargetType="{x:Type ListBox}">

    <Setter Property="ItemTemplate">

    <Setter.Value>

    <DataTemplate>

    <Expander Name="itemExpander" Background="AliceBlue" HorizontalAlignment="Left" ExpandDirection="Down" IsExpanded="False">

    <Expander.Header>

    <TextBlock FontFamily="Verdana" FontSize="12" Text="{Binding}"/>

    </Expander.Header>

    <Expander.Content>

    <!--<TextBlock TextWrapping="Wrap" Width="{Binding Path=Width, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}" >-->

    <TextBlock TextWrapping="Wrap">

    <TextBlock.Width>

    <Binding Path="Width">

    <Binding.RelativeSource>

    <RelativeSource Mode="FindAncestor" AncestorType="ListBox"/>

    </Binding.RelativeSource>

    </Binding>

    </TextBlock.Width>

    Diese text wird unten dargestellte!!!!

    Aber wenn er zu laaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaang ist

    sollte er eugentlifh umebrichen werden !

    </TextBlock>

    </Expander.Content>

    </Expander>

    </DataTemplate>

    </Setter.Value>

    </Setter>

    </Style>

     

     

    I want to reduce the width of the TextBlock for 30px, because of the scrollbar of the ListBox containing this item.

     

    THX!

     

    Mario

    Wednesday, April 16, 2008 3:09 PM

Answers

  • You can do such things with MultiBindings. A MultiBinding is something like a function- call in XAML. Each Binding inside is a parameter of the function. What is done with the parameters is defined in the "Converter"- Class

     

    It looks like:

    <ContentPresenter.Width>

    <MultiBinding Converter="{StaticResource Calc}" ConverterParameter="DivideAbsolute">

    <Binding RelativeSource="{RelativeSource AncestorType={x:Type basicControls:BasicWPFControl}}" Path="ActualWidth"/>

    <Binding RelativeSource="{RelativeSource AncestorType={x:Type basicControls:BasicWPFControl}}" Path="Zoom"/>

    </MultiBinding>

    </ContentPresenter.Width>

    In this case ActualWidth is divided by Zoom. The result is binded to the Width- Property

     

    The problem is that there is no default converter for these cases so you have to write it on your own. It's quite easy to create it, but in your case I think there's a better and much easier solution:

     

    I modified your Style a little bit:

     

    <Style x:Key="gstListBox" TargetType="{x:Type ListBox}">

    <Setter Property="ItemTemplate">

    <Setter.Value>

    <DataTemplate>

    <Expander Name="itemExpander" Background="AliceBlue" HorizontalAlignment="Left" ExpandDirection="Down" IsExpanded="False">

    <Expander.Width>

    <Binding Path="ActualWidth">

    <Binding.RelativeSource>

    <RelativeSource Mode="FindAncestor" AncestorType="ListBox"/>

    </Binding.RelativeSource>

    </Binding>

    </Expander.Width>

    <Expander.Header>

    <TextBlock FontFamily="Verdana" FontSize="12" Text="{Binding}"/>

    </Expander.Header>

    <Expander.Content>

    <TextBlock TextWrapping="Wrap" Margin="0,0,30,0">

    Diese text wird unten dargestellte!!!!

    Aber wenn er zu laaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaang ist

    sollte er eugentlifh umebrichen werden !

    </TextBlock>

    </Expander.Content>

    </Expander>

    </DataTemplate>

    </Setter.Value>

    </Setter>

    </Style>

     

    The Binding of "Width" is done on the expander object to get the correct size. To avoid that text is under the scroll bar I just used the margin property.

    This way you don't need to do any MultiBinding and converters

    Wednesday, April 16, 2008 11:00 PM