|
You've added an ObservableCollection when it's not needed. You can bind directly to a generic list, so adding the binding to the ObservableCollection is unecessary; the only reason to bind to ObservableCollection is to receive notifications when the list changes, which you can mimic without an ObservableCollection (after all, you're maintaining the same list twice here).
As an item is added to the List, all you need to do is raise a CollectionChanged event when an item is added or removed from the list."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
|
|
|
|
|
Yes, you are right. I'm aware of that, that's why I mentioned that the DisplayMemberBinding works when adding items to the Items manually. I tried both ways in a sample code before I first replied.
This has just popped into my mind while I was reading your post, if it's not possible to make the List implement the INotifyCollectionChanged (If it's a class that inherits from List, which is what I understood from the question and the class is not to be touched. But I don't think implementing the interface would break existing code) then maybe creating a wrapper for (or inheriting from) the List (a custom List or just a List), implementing INotifyCollectionChanged and substituting the List with its wrapper (which would be implementing the interfaces of the original List) would be a better option. I haven't thought much about it and I'm interested to know what you think about it.Eslam Afifi
|
|
|
|
|
That would work. I'm currently writing an extended version of ObservableCollection that supports undo functionality, and it's simply a Collection wrapped up using INotifyCollectionChanged."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
|
|
|
|
|
Nice. I'm looking forward to seeing it. Eslam Afifi
|
|
|
|
|
I'm pretty damned sure that John said he couldn't use an ObservableCollection, and he explained why he couldn't use it so your post added nothing to his question."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
|
|
|
|
|
John - you only need an ObservableCollection if you want to catch notification changes. In this case, you'd bind to the properties in exactly the same way you'd bind to an ObservableCollection. Here's a quick sample I whipped together for you:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace BindToList
{
public partial class Window1 : Window
{
private List<MyListItem> _list;
public Window1()
{
InitializeComponent();
_list = new List<MyListItem>();
AddItem("John", "Simmons");
AddItem("Pete", "O'Hanlon");
AddItem("Clint", "Eastwood");
foreach (MyListItem item in _list)
{
listBound.Items.Add((MyListItem)item);
}
}
public List<MyListItem> MyList
{
get { return _list; }
set { _list = value; }
}
public void AddItem(string forename, string surname)
{
_list.Add(new MyListItem { ID = _list.Count + 1, Forename = forename, Surname = surname });
}
}
public class MyListItem
{
private int _id;
public int ID
{
get { return _id; }
set { _id = value; }
}
private string _forename;
public string Forename
{
get { return _forename; }
set { _forename = value; }
}
private string _surname;
public string Surname
{
get { return _surname; }
set { _surname = value; }
}
}
} Now, here's the XAML that goes with this:
<Window x:Class="BindToList.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">
<Window.Resources>
<DataTemplate x:Key="myListTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ID}" />
<TextBlock Text="{Binding Forename}" />
<TextBlock Text="{Binding Surname}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox x:Name="listBound"
ItemTemplate="{StaticResource myListTemplate}"
Width="200" />
</Grid>
</Window> "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
|
|
|
|
|
Hello all,
I am quite new in wpf and i am having some problem in displaying
an System.Windows.forms.Form as child of my WPF main window
I have a WPF application opening some system.windows.forms.form from WPF menu
but the problem is i am unable to make those winforms as child of WPF Main Window
because of which i cant show the winform as center parent.
Can anybody help me out how to make the WPF Window parent of the windows.forms.formabhinav
|
|
|
|
|
You need to add the WindowsFormsIntegration namespace to your project. For more info, google "WPF Winforms integration". There are many good examples out there..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
|
|
|
|
|
thanks John for ur quick reply
can you please provide me some helpful links
actually i searched a lot but didnt found any apropriate solutionabhinav
|
|
|
|
|
Well, I don't know what you want to do (and I don't really care), so you're going to have to come up with your own google phrase. The one I provided is merely a starting point.
Besides that, if you're not actually co-mingling winforms controls on a WPF form (or the other way around), there really is no magic involved that I can think of. Just create the winforms form and be done with it..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, if you want to know just read his post. It's very simple to read and very simple to understand (or do not make this kind of answer). This is a very specific problem and google will not help.
And abhinav the WindowsFormsIntegration.dll cannot help you for your problem.
Just try to listen Application.Current.Activated and Deactivated event to change the TopMost value of the form. I guess it can be a good beginning...
Need help ? Ask me
|
|
|
|
|
Hi,
I'm new to Threading and WPF and need some help Adding to my Combo Box. I Understand I can't touch the UI from a background thread I just cant seem to figure out the work around. I got This Far. You'll see a Combo1.Items.Add(f); That is throwing.
If I can get a good example of how you are suppose to do something like this I'll have a chunk of code I can play with and learn from.
Thank you very much For any help
private void button1_Click(object sender, RoutedEventArgs e)
{
string Server;
Server = cmdServers.SelectedItem.ToString();
FillApplicationCombo(Server);
}
void FillApplicationCombo( string Server)
{
int count;
string Path;
int Version;
string AppName;
Path = @"\\" + Server + @"\" + "DOCFOCUS";
progressBar1.Minimum = 0;
progressBar1.Maximum = _DirectoryFiles.Count;
_Worker = new BackgroundWorker();
_Worker.WorkerReportsProgress = true;
_Worker.WorkerSupportsCancellation = true;
_Worker.DoWork += (s, args) =>
{
BackgroundWorker worker = s as BackgroundWorker;
DirSearch(Path);
count = 0;
foreach (string f in _DirectoryFiles)
{
count++;
worker.ReportProgress(count);
AppName = FileVersionInfo.GetVersionInfo(f).ProductName;
if (AppName == null)
continue;
if (AppName.ToUpper().IndexOf("MYAPPNAME") == -1)
continue;
Version = FileVersionInfo.GetVersionInfo(f).ProductMajorPart;
if (Version == 6)
{
Combo1.Items.Add(f);
}
}
};
_Worker.RunWorkerCompleted += (s, args) =>
{
progressBar1.Value = 0;
};
_Worker.ProgressChanged += (s, args) =>
{
progressBar1.Value = args.ProgressPercentage;
};
_Worker.RunWorkerAsync();
} Ronald Hahn, CNT - Computer Engineering Technologist
New Technologies Analyst
HahnTech Affiliated With Code Constructors
Edmonton, Alberta, Canada
Email: rhahn82@telus.net
|
|
|
|
|
You have to setup a delegate,
private delegate void DelegateUpdateCombo();
create a method that matches it,
private void MyMethod()
{
}
and then use this in your thread event handler:
DelegateUpdateCombo method = new DelegateUpdateCombo(MyMethod);
comboBox.Dispatcher.Invoke(myMethod); .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
|
|
|
|
|
Thanks. That did it Ronald Hahn, CNT - Computer Engineering Technologist
|
|
|
|
|
|
You can easily add external resources in. All you need to do is use the special syntax for loading XAML from external resources. Here's an example of creating 3 resource dictionary entries from 2 DLLs (one called MyOtherAssembly, the other called YetAnotherAssembly):
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyOtherAssembly;component/ResourceDictionary1.xaml" />
<ResourceDictionary Source="/MyOtherAssembly;component/ResourceDictionary2.xaml" />
<ResourceDictionary Source="/YetAnotherAssembly;component/ResourceDictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary> "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
|
|
|
|
|
|
Check to see what namespaces you have in there."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
|
|
|
|
|
I found no answer to this problem yet.
I have a Collection with customer data. One of the properties of the customer class is a collection of addresses. I can bind these collection of addresses to a combobox or listbox with the ItemsSource property.
But what I really want to do is bind some of these addresses to different textboxes. ex:
TextBox1.Text="{binding Customer.Addresses.Address[0]}"
TextBox2.Text="{binding Customer.Addresses.Address[1]}"
...
I hope this is possible...
Also is it possible to sort or filter in this binding scenario?
Many thanks for your thoughts.
|
|
|
|
|
ddecoy wrote: TextBox1.Text="{binding Customer.Addresses.Address[0]}"
TextBox2.Text="{binding Customer.Addresses.Address[1]}"
I suspect you mean this:
<TextBox Text="{Binding Customer.Addresses[0].Address}" />
<TextBox Text="{binding Customer.Addresses[1].Address}" /> "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
|
|
|
|
|
Yes.
But is it possible ?
|
|
|
|
|
Dear Friends.
I want to make a custom control for wpf that a custom combobox. I need to change combobox item drawing. how can I change it from code or access to XAML code of combobox in custom control project.
(I'm using this code for inherit combobox:
Public Class CustomControl1
Inherits ComboBox
)Regards.
Mehdi Ghiasi
|
|
|
|
|
Generally if you think that you want to change the appearance/behaviour of a control in XAML by creating a subclass, this means that you're approaching the problem in the wrong way. As the standard WPF controls are "lookless", this means that you can change the template to suit your own purposes.
The only time that you should even consider inheriting is if you want to radically alter the behaviour of the underlying control - in other cases, you could consider implementing the functionality with Attached Properties/Attached Behaviours."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
|
|
|
|
|
So, How can I make a Color Picker Combo Box (Color Picker Drop Down list) for my WPF Application? (I can make it in my program with both XAML and code. but in a class, I haven't XAML, how can I Change ItemTemplate of ComboBox? I've made this control for windows forms with DrawItem Event.)Regards.
Mehdi Ghiasi
|
|
|
|
|
Dear Friends,
I have small Clarification with MVVM patten With Wpf
I have an User control "A.xaml" , and i am having all the Binding in ViewModel .. "AViewModel.cs" as septate class
i have no code in code behind file "a.cs"
now my problem is like these
in the user control i having 6 buttons, and i want property as "ButtonNo"
if i specify <Usercontrol:A ButtonNo =4/> in Main Window then i have to enable 1,2,3,4 buttons ...
( i know that by writing code in "a.cs" is possible .. but i do not want to write in code behind file .. i have to do it in "AViewModel.cs"
is it possible? if so how to do that?
)
Give me some idea..
By
Joe
|
|
|
|