Results 1 to 7 of 7

Thread: Wpf Databinding

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Wpf Databinding

    Two questions about databinding. First of all I am trying to create a databinding to application settings. I have gone through about 10 tutorials and they all look like mine but its not working. First thing I did in BLEND 3 was add a textbox. Then I went into VISUAL STUDIO and added a user->setting named textboxText of type string. Then I went back over to BLEND and clicked on the button next to the text property and added a databinding to the textboxText and set it to TwoWay. Then in my Window Closing event I added "Properties.Settings.Default.Save();". However when I run the project again it has the same initial setting. Any ideas why? Here is my code generated by BLEND:

    Code:
    <Window
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	xmlns:WpfApplication14_Properties="clr-namespace:WpfApplication14.Properties" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    	x:Class="WpfApplication14.MainWindow"
    	x:Name="Window"
    	Title="MainWindow"
    	Width="640" Height="480" mc:Ignorable="d" Closing="Window_Closing">
    
    	<Window.Resources>
    		<WpfApplication14_Properties:Settings x:Key="SettingsDataSource" d:IsDataSource="True"/>
    	</Window.Resources>
    
    	<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SettingsDataSource}}">
    		<TextBox HorizontalAlignment="Left" Margin="93,110,0,0" VerticalAlignment="Top" Width="120" Height="39" Text="{Binding textboxText, Mode=TwoWay, UpdateSourceTrigger=Explicit}" TextWrapping="Wrap"/>
    	</Grid>
    </Window>

    The other question that I have is can you databind to a variable or even a property. What I mean by that is say I set an integer value named myInteger to 5. Can I then display that in a textbox? If so an example or tutorial would be great.
    If I helped you please rate me.

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Wpf Databinding

    Yeah you can bind to a public property but not to a private property or any kind of variable/field. Here's an example:

    Code:
    <Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300" Name="Window1"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        >
        <Grid>
            <TextBox Name="TextBox1" Text="{Binding Path=MyText}" Height="30" Width="100" />
        </Grid>
    </Window>
    and here is the entire code behind file:
    vb.net Code:
    1. Class Window1
    2.  
    3.     Private _MyText As String = "test"
    4.  
    5.     Public Property MyText() As String
    6.         Get
    7.             Return _MyText
    8.         End Get
    9.         Set(ByVal value As String)
    10.             _MyText = value
    11.         End Set
    12.     End Property
    13.  
    14. End Class

    Note the DataContext line in the XAML (part of the Window attributes at the top) - you dont need to do this if you set your binding in code but if you just do it like this then it doesnt seem to work without that line. I asked about it here and the only solution I got really was to just set binding sources in code and so thats what I do: http://social.microsoft.com/Forums/e...5-510b73d414ed
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: Wpf Databinding

    What if the property is in a different class? Would it be :

    Code:
    <TextBox Name="TextBox1" Text="{Binding Path=Class.Property}" Height="30" Width="100"/>
    If I helped you please rate me.

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Wpf Databinding

    I dont know I've never tried that but I guess it would have to be a shared property though. Give it a go and see what happens
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2009
    Posts
    448

    Re: Wpf Databinding

    Also can you give me a good tutorial or explanation of what a datacontext is? I know what databinding is (just learning how I can use it) but I just cant find any info on what a datacontext actually is.
    If I helped you please rate me.

  6. #6
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    269

    Re: Wpf Databinding

    Quote Originally Posted by chris128 View Post
    Yeah you can bind to a public property but not to a private property or any kind of variable/field. Here's an example:

    Code:
    <Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300" Name="Window1"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        >
        <Grid>
            <TextBox Name="TextBox1" Text="{Binding Path=MyText}" Height="30" Width="100" />
        </Grid>
    </Window>
    and here is the entire code behind file:
    vb.net Code:
    1. Class Window1
    2.  
    3.     Private _MyText As String = "test"
    4.  
    5.     Public Property MyText() As String
    6.         Get
    7.             Return _MyText
    8.         End Get
    9.         Set(ByVal value As String)
    10.             _MyText = value
    11.         End Set
    12.     End Property
    13.  
    14. End Class

    Note the DataContext line in the XAML (part of the Window attributes at the top) - you dont need to do this if you set your binding in code but if you just do it like this then it doesnt seem to work without that line. I asked about it here and the only solution I got really was to just set binding sources in code and so thats what I do: http://social.microsoft.com/Forums/e...5-510b73d414ed
    Can anybody tell me what I need to do if I am creating a usercontrol which will have a datacontext of a business object yet I still want to bind to the actuall controls puplicproperty "ShowButtons" that I am creating. I want to be able to display buttons or not on my usercontrol from it's property?

    Cheers.

  7. #7
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    269

    Re: Wpf Databinding

    Quote Originally Posted by Moorzee View Post
    Can anybody tell me what I need to do if I am creating a usercontrol which will have a datacontext of a business object yet I still want to bind to the actuall controls puplicproperty "ShowButtons" that I am creating. I want to be able to display buttons or not on my usercontrol from it's property?

    Cheers.
    I sussed it. Needed to implement INotifyPropertyChanged intereface in my usercontrol then bind direct to that using the following binding
    Code:
    Visibility="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}, Path=ShowButtons, Converter={StaticResource VisibiltyConverter}}"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width