6 Replies - 1376 Views - Last Post: 28 March 2012 - 04:07 AM Rate Topic: -----

Topic Sponsor:

#1 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 276
  • Joined: 22-May 08

WPF/Winforms databinding

Posted 03 February 2012 - 05:23 AM

I can't seem to get this effect working with either winforms or WPF:

public ObservableCollection<String> NamesWithLetterA
{
    get
    {
        return new ObservableCollection<String>(Model.Names.Where(name => name.Contains('a')).toList());
    }

    set
    {
        PropertyChanged("Names");
    }
}



excuse any coding errors. this example is for wpf; I would be using List<> and bindingsource in winforms. this code would be in a pseudo viewmodel, with Model being the backing datasource.

basically, anytime the backing field changes(which is everytime get is called), the databinding fails. I'm not sure what is going on in the background, but i just want to know if i can get this effect working. I hope its obvious what this effect is.

Is This A Good Question/Topic? 0
  • +

Replies To: WPF/Winforms databinding

#2 eclipsed4utoo  Icon User is online

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1451
  • View blog
  • Posts: 5,763
  • Joined: 21-March 08

Re: WPF/Winforms databinding

Posted 03 February 2012 - 06:09 AM

Instead of showing us just a little bit of code, how about show us the ENTIRE WPF example. Maybe the problem is in another part of your code.
Was This Post Helpful? 0
  • +
  • -

#3 RexGrammer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 152
  • View blog
  • Posts: 664
  • Joined: 27-October 11

Re: WPF/Winforms databinding

Posted 03 February 2012 - 06:14 AM

A thing to note: you're looking for words with the lower case letter a, and you can miss words with the upper case letter A.

The solution:

Convert the name to upper case letter and then look for the letter 'A'. That way you won't miss words with lower case or upper case letters.

Model.Names.Where(name => name.ToUpper.Contains('A')).toList());


Was This Post Helpful? 1
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,896
  • Joined: 02-June 10

Re: WPF/Winforms databinding

Posted 03 February 2012 - 07:18 AM

Are you sure you want .Contains and not .StartsWith?

I mean I see where you were going with this. If you DON'T look for lower case, then you only get names that contain an UPPERCASE letter, and therefore a name that starts with that letter. So just use the StartsWith method.

Is this really a good choice for a property? Its really more a candidate for a method, isn't it?

For example: Can you really set this? You have a get and set method within the property, but you don't handle setting correctly. If someone tried to do this for example

NamesWithLetterA = new[]{"Apple", "Ardvark", "Adam", "Andrea", "Arlene", "Adam Ant"};


Your property wouldn't actual set those, would it? Yet it is going to throw an event saying the property changed - when it didn't. This is going to be real bad news to whomever is using this code.

Since you're really just using it as a method, code it as a method

public ObservableCollection<String> NamesStartingWithA
{
    return new ObservableCollection<String>(Model.Names.Where(name => name.ToUpper.StartsWith('A')).toList());
}




Now you get all names that start with a, even if they were input as lower case by mistake.

But... Like this implies you are going to make 26 of these, virtually all the same. Bad, bad bad. We don't duplicate code when we can make a method that takes a parameter and have just one instance of the code.

public ObservableCollection<String> NamesStartingWith(char letter)
{
    return new ObservableCollection<String>(Model.Names.Where(name => name.ToUpper.StartsWith(letter.ToUpper())).toList());
}



Now you don't need 26 method clones. You just call this one with the right letter as a parameter.

This post has been edited by tlhIn`toq: 03 February 2012 - 07:24 AM

Was This Post Helpful? 0
  • +
  • -

#5 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 276
  • Joined: 22-May 08

Re: WPF/Winforms databinding

Posted 03 February 2012 - 04:55 PM

oh crap.

The point wasn't to make this specific example work. I just wanted to have "Names.Get" return a collection that is dynamically generated.

at the moment, this is what I have that DOES NOT work:
//CODE BEHIND
public partial class MainWindow : Window
    {
        ViewModel VM;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            VM = new ViewModel();

            VM.Names = new ObservableCollection<String> { "real", "notreal", "face" };

            listBox.DataContext = VM.Names;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var newNamesSet = new ObservableCollection<String> { "tweet", "book", "twitter" };
            VM.Names = newNamesSet;
        }
    }

    class ViewModel : INotifyPropertyChanged
    {
        #region Bindable Properties

        private ObservableCollection<String> _names;
        public ObservableCollection<String> Names
        {
            get
            {
                _names = new ObservableCollection<string>(_names.Where(name => name.Length == 4));

                return _names;
            }

            set
            {
                if (_names != value)
                {
                    _names = value;
                    NotifyPropertyChanged("Names");
                }
            }
        }

        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }



//XAML
<Window x:Class="WPFViewer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="431" Width="574" Loaded="Window_Loaded">
    <Grid>
        <ListBox Name="listBox" ItemsSource="{Binding}"/>
        <Button Content="Button" Height="103" HorizontalAlignment="Left" Margin="410,277,0,0" Name="button1" VerticalAlignment="Top" Width="111" Click="button1_Click" />
    </Grid>
    
</Window>



PLEASE just ignore the LINQ query. I just put that there to show I wanted to use LINQ queries just in case it might affect something.

what happens at load time is I see only "real" and "face" in the listbox. When I click the button, I expect to see "book". but nothing changes after I press the button.

This post has been edited by The Architect 2.0: 03 February 2012 - 04:55 PM

Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is online

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1451
  • View blog
  • Posts: 5,763
  • Joined: 21-March 08

Re: WPF/Winforms databinding

Posted 03 February 2012 - 08:55 PM

The reason I asked for code is because you can't ask a theoretical "why doesn't this work?" question about theoretical code. MVVM works, so it would have to be something with your code. Without seeing the code, we can't tell you why your code isn't work.
Was This Post Helpful? 0
  • +
  • -

#7 kiblehana  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-August 11

Re: WPF/Winforms databinding

Posted 28 March 2012 - 04:07 AM

Hello
you can also go for http://www.blog.dapf...om/data-binding to get all information theoretical as well as code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1