|
Thanks Pete O'Hanlon.
I have written a method and I posted here[^] to do that.
It works ,
What's your idea about it ?
|
|
|
|
|
While it will work, it's not a particularly effective method and is bound very much in the way that win forms used to work. What happens though, if you apply virtualisation to the treeview?
"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
|
|
|
|
|
What did you mean by virtualisation ?
|
|
|
|
|
I mean something like this[^].
"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
|
|
|
|
|
Hi !
I am tring to make a form which contains two combo boxes, one for country and one for city.
I have linked them to sql tables i have and used linq to sql.
I got to bind each to its table, but can't filter the cities by country.
Please help....
|
|
|
|
|
You should get a good idea on how to handle this here[^].
"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
|
|
|
|
|
This seems to be great and it is solving exactly the problem I'm facing,
BUT all the answers are in C## and i am writing VB.....
Is there an answer for me in VB anywhere ?
|
|
|
|
|
Hello
Can you bind a single hashtable (hashtable is a collection of objects) item to the fill property of a path control.
I want to bind a value of an item which is stored in a hashtable (i am using a third party control which returns a hashtable)
to the fill property of a path. Then using a converter change the colour of the path fill propety.
Now i have tested this a number of ways but cannot get it to work. The hashtable is a collection of objects which i have written the class for. My converter is currently quite simple, converting from type short to a brush type. I have tested this and it works when binding to another element.
But i cannot get the binding syntax correct for indexer binding.
Does anyone have any examples of this.
Thanks
|
|
|
|
|
I think the problem is that when you attempt to bind to a hastable, WPF for some reason assumes that the value you pass in as the key is a string rather than an integer. One solution would be to have the keys in your hashtable be strings rather than numeric types (assuming you are currently using a numeric type). You could also convert your hashtable to a Dictionary, which is strongly typed and which WPF seems to have no problem binding to. However, it seems you were given the hashtable, so maybe you don't want to modify the collection. The alternative would be to implement IValueConverter and pass in the hash table as the value to convert and the key as the parameter. The converter would then get the value at that key. You will want to make sure the converter actually uses a key of the correct type. So, if the XAML passes in a string but the key is supposed to be an integer, you will want to use int.Parse to get the integer before using it on the dictionary. I will leave implementing the IValueConverter as an exercise for the reader (namely, you), but here is the alternative approach I talked about:
public System.Collections.Hashtable MyHashTable
{
get
{
System.Collections.Hashtable table = new System.Collections.Hashtable();
table["5"] = new SolidColorBrush(Colors.Red);
return table;
}
}
<Window
x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="MyWindow"
Title="Window1">
<Grid>
<Path
Data="M 100,200 C 100,25 400,350 400,175 H 280"
Fill="{Binding ElementName=MyWindow, Path=MyHashTable[5]}" />
</Grid>
</Window>
As one final note, you could also create your own Dictionary type that just wraps a hastable. So, you pass the hashtable to the constructor of your HashWrapper (which acts like a strongly typed Dictionary) and when a key is passed to it, it just passes that key on to the wrapped hashtable and then casts the result into the correct type. The converter option is probably better than this one, but I thought I'd mention this one in case it matched up with your skills more appropriately.
|
|
|
|
|
Hello
Thankyou for that. I had been pulling my hair out for two days prior to your post. The important piece of information i was missing was that the key was taken as a string.
I was able to change the Key from an integer to a string, which is actually what i wanted anyway. Here is the XAML below
Path Name="P056" Fill="{Binding ElementName=MyPage,Path = MyHashTable[Tag_1].Value ,Converter={StaticResource StatustoColorConverter},UpdateSourceTrigger = PropertyChanged}" Stroke="DarkGreen" StrokeThickness="1" Width="27.684" Height="35.374" Data="M 5,5 c 9,0 13,8,13,13.5 c 0,5.5 -5,13 -12.5,13 v -5 c 5,0 7,-4 7,-8 c 0,-4 -4,-8 -7,-8.3 v-5 " Canvas.Left="100" Canvas.Top="100";/Path;
|
|
|
|
|
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
|
|
|
|