[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Dynamically changing properties in XAML such as Width, FontSize, etc for a TextBox

Posted on 2010-11-30
4
Medium Priority
?
821 Views
Last Modified: 2012-05-10
Hi All,

I am working on my Visual aspect of my controls right now. I am trying to dynamically change certain properties on the MouseOver and IsFocused ControlTemplate triggers of a TextBox.

I want to change the Width to the Current TextBox Width + 10 and FontSize to Current TextBox FontSize + 2 when the MouseOver trigger is True.

I can not figure out a way to do this in XAML.

I know how to do this easily in C#, but I am trying to keep all of this code in XAML.

How do I do this in a trigger????

<Setter Property="Width" Value="Width+20" />

Thanks in advance.
<Style x:Key="bhTextBoxStyle" x:Name ="bh1" TargetType="{x:Type BH:bh_TextBox}">
        <Setter Property="SnapsToDevicePixels"
            Value="True" />
        <Setter Property="OverridesDefaultStyle"
            Value="True" />
        <Setter Property="KeyboardNavigation.TabNavigation"
            Value="None" />
        <Setter Property="FocusVisualStyle"
            Value="{x:Null}" />
        <Setter Property="FontSize" Value="{DynamicResource bh_TextBoxFontSize}" />
        <Setter Property="Foreground" Value="{DynamicResource bh_ControlOffBlack}" />
        <Setter Property="Padding" Value="2,5" />
        <Setter Property="IsTabStop" Value="True" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="MinWidth"
            Value="1" />
        <Setter Property="MinHeight"
            Value="20" />
        <Setter Property="AllowDrop"
            Value="true" />
        <Setter Property="HorizontalContentAlignment" Value="Left" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate x:Name="bh_ct_textbox" TargetType="{x:Type BH:bh_TextBox}">
                    <Border Name="Border"
                  CornerRadius="10"
                  Padding="0"
                  BorderThickness="1"
                  Background ="{DynamicResource bh_TextBoxBrush}"
                  BorderBrush="{DynamicResource bh_TextBoxBorderBrush}">

                        <my:VisualStateManager.VisualStateGroups>
                            <my:VisualStateGroup x:Name="CommonStates">
                                <my:VisualState x:Name="Normal" />
                                <my:VisualState x:Name="Disabled">
                                    <Storyboard>

                                    </Storyboard>
                                </my:VisualState>
                                <my:VisualState x:Name="ReadOnly">
                                    <Storyboard>
                                    </Storyboard>

                                </my:VisualState>
                                <my:VisualState x:Name="MouseOver">
                                  <Storyboard Name="TextBoxFade" AutoReverse="False">
                                    <DoubleAnimation  Name="TextBoxFadeAnimation"
                                            Storyboard.TargetName="Border" 
                                            Storyboard.TargetProperty="(Border.Opacity)"
                                            From="0.4" To="1.0" Duration="0:0:1" 
                                            AutoReverse="False" RepeatBehavior="1x" 
                                         />
                                    </Storyboard>
                                </my:VisualState>
                            </my:VisualStateGroup>
                        </my:VisualStateManager.VisualStateGroups>
                        <ScrollViewer Margin="0"
                          x:Name="PART_ContentHost" />
                    </Border>
                    
                    <ControlTemplate.Triggers>
   
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName ="Border" Property="BorderBrush" Value="{DynamicResource bh_MouseOverBorderBrush}" />
                            <Setter TargetName="Border" Property="BorderThickness" Value="3" />
                            <Setter Property="Width" Value="100" />
                            <Setter Property="FontSize" Value="{DynamicResource bh_TextBoxMouseOverFontSize}" />
                            <Setter Property="Padding" Value="2,1" />
                        </Trigger>

Open in new window

0
Comment
Question by:HOLLYWUB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:ventaur
ID: 34239368
There is no built-in way to handle expressions in your binding markup. I suggest you use the custom LambdaConverter to accomplish expression handling in your XAML.

Then, setup a resource (perhaps with a Key of "addtion") using the LambdaConverter with an expression of "value+parameter". Finally, your binding should use TemplateBinding to get at the control's own Width property.

<Window ...
  xmlns:XamlConverter="clr-namespace:XamlConverter;assembly=XamlConverter"
  ...>
  <Window.Resources>
    <XamlConverter:LambdaConverter x:Key="addition">value+parameter</XamlConverter:LambdaConverter>
  </Window.Resources>

...
  <Setter Property="Width" Value="{TemplateBinding Property=Width, Converter={StaticResource addition}, ConverterParameter='10'}" />
...

Open in new window

0
 

Author Comment

by:HOLLYWUB
ID: 34251077
Hi Ventaur,

I am getting an Assembly not found error for XamlConverter.

What reference do I need to add to my project for the XamlConverter?

0
 
LVL 6

Accepted Solution

by:
ventaur earned 1000 total points
ID: 34252578
Please, note that I have not used the LambdaConverter myself. That said, the home page for the project mentions needing the Dynamic.cs file from some samples included with VS 2008 or the provided download link. It sounds like you should include that source file and the LamdaConverter.cs source file in your project somewhere in order to get all this working.
0
 

Author Closing Comment

by:HOLLYWUB
ID: 34375107
The expert did not actually try this solution first. It didn't work. I went another route after searching other FREE knowledge bases.

I was very surprised I did not get a qualified soluition for this question.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question