|
I have an object with some properties.
It looks something like this in the code:
public class MyForm
{
MyObject myObject = new MyObject();
public MyForm()
{
myObject.Property1 = true;
}
}
I want to bind these properties to controls on a form.
I've tried something like this in the form's XAML:
<CheckBox Name="checkBox1" IsChecked="{Binding Path=myObject.Property1}" />
What am I doing wrong?
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
John,
There are a few way you can do this. Normally I would assign say the Grid’s DataContext in your example to the MyObject property on your MyForm class.
Alternatively, you can use the ElementName attribute of the binding to the Window, and the property will be resolved like so:
<Window x:Class="WpfApplication4.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"
x:Name="Root">
<Grid>
<CheckBox Name="checkBox1" IsChecked="{Binding ElementName=Root, Path=MyObject.Property1}" />
</Grid>
</Window>
public partial class Window1 : Window
{
MyObject myObject = new MyObject();
public MyObject MyObject
{
get
{
return myObject;
}
}
public Window1()
{
InitializeComponent();
myObject.Property1 = true;
}
}
public class MyObject : INotifyPropertyChanged
{
bool property1;
public bool Property1
{
get
{
return property1;
}
set
{
property1 = value;
OnPropertyChanged(new PropertyChangedEventArgs("Property1"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
}
Cheers,
Daniel
|
|
|
|
|
Is it possible to derive from DependencyObject and create a dependency property instead of implementing INotifyPropertyChanged ?
Greetings - Jacek
|
|
|
|
|
|
You would still need INotifyPropertyChanged to notify UI, right?
There are only 10 types of people in this world — those who understand binary, and those who don't. |
|
|
|
|
|
Not if the properties are dependency properties.
|
|
|
|
|
I think you can only bind to public properties - make sure Property1 is a public property.
You can also try specifying the DataContext explicitly.
public MyForm()
{
myObject.Property1 = true;
checkbox1.DataContext = myObject;
}
<CheckBox Name="checkBox1" IsChecked="{Binding Property1}" />
|
|
|
|
|
Hi.
I have written below XAML code :
<ScrollViewer Grid.Row="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<ListView x:Name="ListView1" Background="#FFEEF3FA" GridViewColumnHeader.Click="ListViewSort_Click" ItemsSource="{Binding ListViewItemsCollections}" SelectionChanged="ListView1_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn x:Name="GridViewColumnName" Header="Name" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image x:Name="ImageListViewItem" Width="16" Height="16" Source="{Binding GridViewColumnName_ImageSource}" />
<Label Content="{Binding GridViewColumnName_LabelContent}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="GridViewColumnTags" Header="Tags" Width="100" DisplayMemberBinding="{Binding GridViewColumnTags}" />
<GridViewColumn x:Name="GridViewColumnLocation" Header="Location" Width="238" DisplayMemberBinding="{Binding GridViewColumnLocation}" />
<GridViewColumn x:Name="GridViewColumnVisitCount" Header="Visit Count" Width="0" DisplayMemberBinding="{Binding GridViewColumnVisitCount}" />
<GridViewColumn x:Name="GridViewColumnVisitDate" Header="Visit Date" Width="0" DisplayMemberBinding="{Binding GridViewColumnVisitDate}" />
<GridViewColumn x:Name="GridViewColumnAdded" Header="Added" Width="0" DisplayMemberBinding="{Binding GridViewColumnAdded}" />
<GridViewColumn x:Name="GridViewColumnLastModified" Header="Last Modified" Width="0" DisplayMemberBinding="{Binding GridViewColumnLastModified}" />
</GridView>
</ListView.View>
</ListView>
</ScrollViewer>
As you can see the name ImageListViewItem exists in the code , but whenever I'm gonna compile the project I receive this error :
Error 1 The name 'ImageListViewItem' does not exist in the current context D:\My Works\C#\WPF\MainWindow.xaml.cs 704 29 passwordManager
What's wrong with it ?
Could you please guide me ?
Thanks.
|
|
|
|
|
Do a build -> clean solution and then try again.
There are only 10 types of people in this world — those who understand binary, and those who don't. |
|
|
|
|
|
I've done it, Unfortunately I receive the error again
|
|
|
|
|
You can try one more thing - copy your entire code, delete this xaml file, add a new one and paste this code in there. It sounds a little crazy, but once in a way it works. The reason is, the temporary code file created for this xaml may have this ImageListViewItem reference missing.
There are only 10 types of people in this world — those who understand binary, and those who don't. |
|
|
|
|
|
Thanks.
But I found another way :
Image img = FindName("ImageListViewItem") as Image;
It works.
|
|
|
|
|
I tested your way. It doesn't work
I receive these errors :
Error 1 The name 'Image_GridViewColumnName' does not exist in the current context
Error 2 Unknown build error, 'Could not find file 'D:\My Works\C#\WPF\passwordManager\obj\Debug\passwordManager.exe'. Line 8 Position 2.'
BTW: I have changed the name to Image_GridViewColumnName
|
|
|
|
|
I agree with the previous poster. Clean the solution, delete the bin and obj folders in each project folder, then rebuild the solution. I tried your XAML and, after I added the event handlers, everything compiled fine.
|
|
|
|
|
The problem you've hit is that you have the name of this item in a cell template. WPF needs to do some tricks to prevent name collisions - hence you can't directly reference this item. You need to read up on name scopes[^].
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
if i compile my wpf project in xp
and execute it on vista.
will it be executing normally.
i had an experience it from windows form application. it was not ruing on vista. i faced color combination problem.
|
|
|
|
|
It's not WPF that you need to consider, but rather whether or not your application violates any of the rules that have been put in place in Vista as a result of the security elevation.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
As Pete said it is a significatly difference security model between these two OSes. Make sure your app doesn't write to it's private installation location and in stead writes to Application Data, that sort of thing.
|
|
|
|
|
Hi Friends
i have created a loader user control in wpf now i want to show loader when calling Webservice and after i get responce i should collapse the Loader control. but it not working what should i do.
WANTED wasim khan(Killed 50 Innocent Buggs, Distroyed 200 Exception, make 5 Project Hostage) any Compnay Hire him will pay 30,000. Best place where u can get him is Sorcim Technologies Murre Road RWP
|
|
|
|
|
wasimsharp wrote: but it not working what should i do.
What's not working?
Make sure your web service is called asynchronously so the UI thread
can show the control. That's just a guess based on the complete lack of
information in your post.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I am attemptimg to programmatically highlight a selection of text in a textbox.
I trying to achieve this by so doing:
tbxItemUrl.SelectionStart = 0;
tbxItemUrl.SelectionLength = pageDetailItemSection.tbxItemUrl.Text.Length;
tbxItemUrl.SelectionBackground = new SolidColorBrush(Colors.Yellow);
tbxItemUrl.SelectionForeground= new SolidColorBrush(Colors.Black);
but to no avail.
some resources...i have attempted to try
[][^]
Has anybody successfully managed to do this...thanks
|
|
|
|
|
I did not have to search far[^]. Hope it helps !
There are only 10 types of people in this world — those who understand binary, and those who don't. |
modified on Friday, January 8, 2010 1:02 PM
|
|
|
|
|
Also, your code probably didn't work because the TextBox didn't have focus.
This worked for me:
System.Windows.Browser.HtmlPage.Plugin.Focus();
tbxItemUrl.SelectionStart = 0;
tbxItemUrl.SelectionLength = tbxItemUrl.Text.Length;
tbxItemUrl.SelectionBackground = new SolidColorBrush(Colors.Yellow);
tbxItemUrl.SelectionForeground = new SolidColorBrush(Colors.Black);
tbxItemUrl.Focus();
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks for the replies.
Really appreciated.
The focus definitely works, thank you, however i have multiple textboxes that i am looking for matches in and would like to highlight any matches in all the necessary textboxes.
Setting the focus would only then highlight 1 textbox.
Any suggestions would be welcome.
Thanks again.
|
|
|
|
|
You'll need to do highlighting something like
Abhinav S linked to above.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|