9 Replies - 2885 Views - Last Post: 29 December 2013 - 02:42 PM Rate Topic: -----

#1 adn258   User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 816
  • Joined: 31-August 11

Data binding In WPF Types Template Vs. Dynamic Vs. Static?

Posted 26 December 2013 - 03:19 PM

Hey guys. So awhile back I said I didn't want to learn WPF and that I hated it, but now I'll admit it DOES seem better and there are some things I really love about it.

One of the things that really got to me (And I know this is a minor side issue) is how well some things really work out of the box. For example a RTB that is set to readOnly really IS READ ONLY! I remember in Winforms if you set a rtb to read only the cursor could STILL be placed in the box even though you couldn't type anything.

This would cause issues with what was in focus and selected even though you had no reason why you wanted the users to select the RTB and place the cursor in there.

Then on top of that I'm getting used to using styles finally in parts of the application etc. references static resources. I do like the looks of things that can create.

I know I still have a LOOOOONNG way to go in terms of really fully grasping WPF but I think it's worth it and I do like what you can do.


That said I've almost been solely using static resource stuff for example things like


 <WrapPanel.Resources>
                <Style TargetType="{x:Type Button}" BasedOn="{StaticResource NumberBtn}" />
 </WrapPanel.Resources>



or


<Style TargetType="{x:Type Button}" BasedOn="{StaticResource NumberBtn}" />



What's the difference between static and dynamic for this? Is it ok to use Static? I've tried to read about the differences but it's confusing?

Also what about template binding? Thanks Guys!

Is This A Good Question/Topic? 0
  • +

Replies To: Data binding In WPF Types Template Vs. Dynamic Vs. Static?

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,457
  • Joined: 05-May 12

Re: Data binding In WPF Types Template Vs. Dynamic Vs. Static?

Posted 26 December 2013 - 06:57 PM

Have you looked at this section of text from MSDN: Static vs. Dynamic Resources
Was This Post Helpful? 2
  • +
  • -

#3 adn258   User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 816
  • Joined: 31-August 11

Re: Data binding In WPF Types Template Vs. Dynamic Vs. Static?

Posted 26 December 2013 - 07:33 PM

Yes I have SkyDiver and I still really don't get it, but it sounds like you should only use dynamic binding if your resource that you're asking it to bind to will be changing a lot right?

What about TemplateResource or TemplateBinding? Sorry this is still confusing to me?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Data binding In WPF Types Template Vs. Dynamic Vs. Static?

Posted 27 December 2013 - 08:08 PM

What book & chapter are you reading about these things from?
I have LOTS of WPF books and can probably look up the area that's confusing you. That way we are both referring to the same resource and wording.
Was This Post Helpful? 0
  • +
  • -

#5 adn258   User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 816
  • Joined: 31-August 11

Re: Data binding In WPF Types Template Vs. Dynamic Vs. Static?

Posted 27 December 2013 - 11:10 PM

toq I apologize but I'll admit I don't have any WPF books. (I'm seriously thinking of buying one though). What do you recommend? I can show you what I'm referring to: For example to finally style my buttons I've used:


 <window.Resources>
        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0" x:Key="DarkBlueGradient">
            <GradientStop Color="#FF3570B8" Offset="0"/>
            <GradientStop Color="#FF265691" Offset="0.379"/>
            <GradientStop Color="#FF0035C1" Offset="0.171"/>
            <GradientStop Color="#FF0022FB" Offset="0.919"/>
            <GradientStop Color="#FF0074FF" Offset="0.71"/>
        </LinearGradientBrush>

        <Style TargetType="Button" x:Key="NumberBtn">
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Height" Value="35"/>
            <Setter Property="Width" Value="35"/>
            <Setter Property="Foreground" Value="White" />
            <Setter Property="FontFamily" Value="Verdana" />
            <Setter Property="FontSize" Value="18" />
            <Setter Property="Margin" Value="2" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border 
           x:Name="Border"  
           CornerRadius="2" 
           BorderThickness="1"
           Background="{DynamicResource DarkBlueGradient}"
           BorderBrush="{DynamicResource DarkBlueGradient}">
                            <ContentPresenter 
                                
             HorizontalAlignment="Center"
             VerticalAlignment="Center"
             RecognizesAccessKey="True"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsKeyboardFocused" Value="true">
                                <Setter TargetName="Border" Property="BorderBrush" Value="Black" />
                            </Trigger>
                            <Trigger Property="IsDefaulted" Value="true">
                                <Setter TargetName="Border" Property="BorderBrush" Value="Black" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="Black" />
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="Black" />
                                <Setter TargetName="Border" Property="BorderBrush" Value="Black" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </window.Resources>
    <Grid>
        <WrapPanel HorizontalAlignment="Left" Height="157" Margin="10,138,0,0" VerticalAlignment="Top" Width="117" Name="NumberBtnPanel">
            <WrapPanel.Resources>
                <Style TargetType="{x:Type Button}" BasedOn="{StaticResource NumberBtn}" />
            </WrapPanel.Resources>

            <Button Content="1" Name="btnOne"/>
            <Button Content="2" Name="btnTwo"/>
            <Button Content="3" Name="btnThree"/>
            <Button Content="4" Name="btnFour"/>
            <Button Content="5" Name="btnFive"/>
            <Button Content="6" Name="btnSix"/>
            <Button Content="7" Name="btnSeven"/>
            <Button Content="8" Name="btnEight"/>
            <Button Content="9" Name="btnNine"/>
            <Button Content="0" Name="btnZero" Width="74" />
            <Button Content="." Name="btnDecimal" />
        </WrapPanel>
        <RichTextBox HorizontalAlignment="Left" Height="89" Margin="10,11,0,0" VerticalAlignment="Top" Width="398" IsReadOnly="True" Name="rtbHistory">
            <FlowDocument>
                <Paragraph>
                    <Run Text=""/>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>
        <RichTextBox HorizontalAlignment="Left" Height="28" Margin="10,105,0,0" VerticalAlignment="Top" Width="398" IsReadOnly="True" Name="rtbInput">
            <FlowDocument>
                <Paragraph>
                    <Run Text=""/>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>
        <WrapPanel HorizontalAlignment="Left" Height="157" Margin="128,138,0,0" VerticalAlignment="Top" Width="41" Name="OperationsPanel">
            <WrapPanel.Resources>
                <Style TargetType="{x:Type Button}" BasedOn="{StaticResource NumberBtn}" />
            </WrapPanel.Resources>
            <Button Content="+" Name="btnPlus" />
            <Button Content="-" Name="btnMinus" />
            <Button Content="*" Name="btnMultiply" />
            <Button Content="/" Name="btnDivide" />
        </WrapPanel>
        <Button Content="=" HorizontalAlignment="Left" Margin="169,219,0,0" VerticalAlignment="Top" Width="39" Height="76" Name="btnEquals"/>




For example in the code above I'm using that button style which references other stuff and each wrap panel references that style.

I feel like I'm progressing slowly in WPF but (OK) considering that I've never done a WPF program EVER and I've never read any books on the topic. I've read stuff online though of course and I've gotten some help from you guys! So using my code above (for example) I've just been using staticreferences and I don't know why one or the other would be better.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Data binding In WPF Types Template Vs. Dynamic Vs. Static?

Posted 28 December 2013 - 07:43 AM

I do see and beleive that you are putting a lot of effort into learning WPF. Next you have to ask yourself "Am I getting the best return on my effort investment?"

Let's face it, there are only 24 hours in a day and they aren't making any more. By the time we are done with sleep, work and life that leaves... what... 5 hours a day to spend on our hobbies, assuming we don't have a husband/wife that wants some of our time.

Of those 5 hours, do you really want to spend 4 of them doing searching and filtering through results looking for a tutorial that might cover one subject you need? Resulting in 10 different authors each with different approaches and styles, supplying your weekly course material.

Spend the money on a couple good WPF books. Work them from cover to cover just learning. Don't try to build or engineer your own program from scratch until you've completed at least one of the books.

Books on learning computer program languages aren't meant to be used by dictionaries where you just look up one thing here and there, when you already know the language. Because you DON'T know the language or its syntax or its underlying concepts yet. They are meant to be read and worked from start to finish, like any other foreign language course.

I'm serious when I say that spending 100 hours to JUST work the book cover to cover will save you time, not consume it. Your current approach of read one tutorial, write your code, read a tutorial, change your code because of new information, read a tutorial, change the underlying architecture because you now understand something better [...] is really very inefficient and frustrating to the developer. Spend the 100 hours on working through a book, then look at your current WPF project fresh and you'll see 1,000 ways it could be better or 'righter' and you'll start version 2 fresh.
Was This Post Helpful? 1
  • +
  • -

#7 adn258   User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 816
  • Joined: 31-August 11

Re: Data binding In WPF Types Template Vs. Dynamic Vs. Static?

Posted 29 December 2013 - 02:30 AM

TOQ I really appreciate your support. I fully agree with you too! At one time I spent most my time reading basic programming related stuff (years ago) mostly related to C and some C++. I don't think that was a waste of time but continuously researching information online as programming concepts get more complicated probably is!

WPF isn't just some beginner basic programming (at least in the broader sense) it encompasses A LOT OF MATERIAL you're totally right. I think I should get a book but I'll admit that I've never been the best at learning stuff by reading Vs. learning stuff by doing. That said I know a lot of these books have exercises which definitely helps.

On a final note TOQ do you think that WPF will still be one of the widely used languages for at least a decent period of time? I guess the thing that scares me the most is learning these languages and only to find 2 years down the road it's all useless and WPF becomes like WinForms (and then I have to learn something else).

Sure I know that some part of being a developer means things constantly evolve but there are some languages and things that have stood the test of time a lot longer. I'm just hoping that WPF is something that at least stays a primary development tool for at least the next 5 years for windows!

I'm not saying that I'm the most amazing expert out there in just general C# related concepts (though at this point I would say I'm ok). I'm at least decently familiar with C# but not really C# with WPF. I'll also admit that I might have been better off getting a basic C# book to start with too!

This post has been edited by adn258: 29 December 2013 - 02:34 AM

Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,457
  • Joined: 05-May 12

Re: Data binding In WPF Types Template Vs. Dynamic Vs. Static?

Posted 29 December 2013 - 07:22 AM

There are no guarantees. Consider how quickly Microsoft abandoned Silverlight in favor of HTML5 even though prior to that all the messaging was that Silverlight was superior to all other browser technologies. The techniques that you do learn do still apply though since all of Silverlight, WPF, and Windows Store Apps all use XAML.

If I were to make a guess, WPF will also get set aside, but XAML will still continue on in yet another form. Personally, I'm hoping for something equivalent to Razor for XAML.
Was This Post Helpful? 1
  • +
  • -

#9 adn258   User is offline

  • D.I.C Addict

Reputation: 12
  • View blog
  • Posts: 816
  • Joined: 31-August 11

Re: Data binding In WPF Types Template Vs. Dynamic Vs. Static?

Posted 29 December 2013 - 02:29 PM

Yeah it's hard to know what to dedicate time too SkyDiver. I agree WPF will be set aside but when? This is one of the core reasons I guess I haven't bought any books on it etc. I figure if I can make a few apps and the XAML isn't perfect oh well!

I'm sort of dodged ASP related stuff in general using PHP for Server Side Technology. That's what most sites use and I think for good reason.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6534
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Data binding In WPF Types Template Vs. Dynamic Vs. Static?

Posted 29 December 2013 - 02:42 PM

I've said it before and I'll say it again... Quit trying to predict the future. None of us are clairvoyant. You are not trying to learn to be a WPF-er for a living. You are working on being a software engineer. You will be learning new languages and technologies for the rest of your life. Accept it. WinForms has been the workhorse of Windows coding for a couple decades. Silveright died after a couple years. WPF is XML based which is going to be around a few more decades in one variant or another. Learning ANY language is good and will make learning the next one that much easier/faster.

For all the time you've spent asking "What language will last for a while?" you could have covered 6 more chapters in a WPF book and learned something instead of worrying about it. Enough whining about it already. You are starting to sound like my kids, who would take 5 hours to try to figure out a way to shave 10 minutes of a 30 minute chore. If you'd just stop looking for ways to avoid the work of learning something you could apply yourself to the actual learning process and get somewhere.

This post has been edited by tlhIn`toq: 29 December 2013 - 02:43 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1