|
-
Aug 7th, 2009, 02:52 PM
#1
Thread Starter
Hyperactive Member
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.
-
Aug 11th, 2009, 07:11 PM
#2
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:
Class Window1
Private _MyText As String = "test"
Public Property MyText() As String
Get
Return _MyText
End Get
Set(ByVal value As String)
_MyText = value
End Set
End Property
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
-
Aug 11th, 2009, 07:28 PM
#3
Thread Starter
Hyperactive Member
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.
-
Aug 11th, 2009, 07:30 PM
#4
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
-
Aug 11th, 2009, 07:32 PM
#5
Thread Starter
Hyperactive Member
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.
-
Jul 1st, 2010, 06:51 AM
#6
Hyperactive Member
Re: Wpf Databinding
 Originally Posted by chris128
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:
Class Window1
Private _MyText As String = "test"
Public Property MyText() As String
Get
Return _MyText
End Get
Set(ByVal value As String)
_MyText = value
End Set
End Property
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.
-
Jul 1st, 2010, 08:43 AM
#7
Hyperactive Member
Re: Wpf Databinding
 Originally Posted by Moorzee
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|