Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 897
  • Last Modified:

WPF: Bind a propertie in a WPF user control

HI!

I'm new to WPF so I need some help.

I'm working on a usercontrol.

I have this code.

   xmlns:apng="clr-namespace:Apng;assembly=ApngImage"
    Title="Window1" Height="200" Width="200" Background="Red" >
    <Grid>
        <Border Margin="10,10,10,10">
            <apng:ApngImage Name="apngControl1" Stretch="Fill" Path="chompy2.png" Fps="25" />
        </Border>
    </Grid>

But the image that I have hard coding here "chompy2.png" I like to set from a propertie instead.

How do I  do that?
0
AWestEng
Asked:
AWestEng
  • 3
  • 2
  • 2
  • +1
5 Solutions
 
Gururaj BadamCommented:


0
 
joriszwaenepoelCommented:
Together with the Binding (see answer from Novice Novice), you also need to set the "DataContext".

For example:

this.DataContext = theObject;
0
 
Gururaj BadamCommented:
@joriszwaenep… - You're right.

I'd assumed that would have been done as the code snippet is not complete
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AWestEngAuthor Commented:
what is this
this.DataContext = theObject;
and where should I put it?
0
 
AWestEngAuthor Commented:

<UserControl x:Class="HostingWpfUserControlInWf.ucAPng"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:apng="clr-namespace:Apng;assembly=ApngImage"
Height="144" Width="172" Background="AliceBlue">
<Grid>
<apng:ApngImage Name="apngControl1" Stretch="Fill" Path="{Binding Path=Image}" Fps="25" />
</Grid>
</UserControl>
0
 
joriszwaenepoelCommented:
theObject => this is the object with a property called "Image" that you want to use for your databinding

You should put that line of code somewhere in your code, where you have created or selected the object that you want to bind to.  It could be the "Loaded" event, or it could be a property setter in case of a usercontrol ...  
0
 
Gururaj BadamCommented:
Original code

 

Modified

 
0
 
rajeshjamnadasCommented:
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now