Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. User Control Question

User Control Question

Scheduled Pinned Locked Moved WPF
questioncomtutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    This is a bit long, and I'm not entirely sure how to word my question... I am developing a prototype for a simple lookup control[^]. The source is here[^]. The Category combo is on the window, and below it is my control. When a category is selected, I want the Lookup control to get the category Id:

    My Lookup control's VM is pretty simple:

    public class LookupComboControlViewModel : _ModelBase
    {
    private List _People = new List();
    public List People
    {
    get { return _People; }
    set
    {
    if (_People != value)
    {
    _People = value;
    RaisePropertyChanged("ComboSourceData");
    }
    }
    }

    private string \_CategoryName;
    public string CategoryName
    {
        get { return \_CategoryName; }
        set
        {
            if (\_CategoryName != value)
            {
                \_CategoryName = value;
                RaisePropertyChanged("CategoryName");
            }
        }
    }
    
    private int \_CategoryId;
    public int CategoryId
    {
        get { return \_CategoryId; }
        set
        {
            if (\_CategoryId != value)
            {
                \_CategoryId = value;
                RaisePropertyChanged("CategoryId");
    
                loadData();
            }
        }
    }
    
    private v
    
    L 1 Reply Last reply
    0
    • K Kevin Marois

      This is a bit long, and I'm not entirely sure how to word my question... I am developing a prototype for a simple lookup control[^]. The source is here[^]. The Category combo is on the window, and below it is my control. When a category is selected, I want the Lookup control to get the category Id:

      My Lookup control's VM is pretty simple:

      public class LookupComboControlViewModel : _ModelBase
      {
      private List _People = new List();
      public List People
      {
      get { return _People; }
      set
      {
      if (_People != value)
      {
      _People = value;
      RaisePropertyChanged("ComboSourceData");
      }
      }
      }

      private string \_CategoryName;
      public string CategoryName
      {
          get { return \_CategoryName; }
          set
          {
              if (\_CategoryName != value)
              {
                  \_CategoryName = value;
                  RaisePropertyChanged("CategoryName");
              }
          }
      }
      
      private int \_CategoryId;
      public int CategoryId
      {
          get { return \_CategoryId; }
          set
          {
              if (\_CategoryId != value)
              {
                  \_CategoryId = value;
                  RaisePropertyChanged("CategoryId");
      
                  loadData();
              }
          }
      }
      
      private v
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Don't you need to bind the SelectedItem property in the Combo to something so you know what is selected?

      K 1 Reply Last reply
      0
      • L Lost User

        Don't you need to bind the SelectedItem property in the Combo to something so you know what is selected?

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        I could be wrong, but I don't think so. The user control is bound to the Category combo's SelectedValue, so when a category is selected, it's value (CategoryId), should be passed to my control.

        views:LookupComboControl Grid.Row="2"
        CategoryId="{Binding ElementName=CategoryCombo, Path=SelectedValue, Mode=TwoWay}"
        Height="70"
        Width="400"/>

        If it's not broken, fix it until it is

        L 1 Reply Last reply
        0
        • K Kevin Marois

          I could be wrong, but I don't think so. The user control is bound to the Category combo's SelectedValue, so when a category is selected, it's value (CategoryId), should be passed to my control.

          views:LookupComboControl Grid.Row="2"
          CategoryId="{Binding ElementName=CategoryCombo, Path=SelectedValue, Mode=TwoWay}"
          Height="70"
          Width="400"/>

          If it's not broken, fix it until it is

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I think you are right in principal, but in practice I'm sure I've seen/had problems with selected value not working as expected... For the sake of a test, change it to bind to the text property and see if it starts working- at least that narrows the issue down!

          K 1 Reply Last reply
          0
          • L Lost User

            I think you are right in principal, but in practice I'm sure I've seen/had problems with selected value not working as expected... For the sake of a test, change it to bind to the text property and see if it starts working- at least that narrows the issue down!

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #5

            OK, I think you were right. The Output window was showing a Null being passed as SelectedValue. So I made the following changes:: In the control:

            public object SelectecCategory
            {
            get { return (object)GetValue(SelectecCategoryProperty); }
            set
            {
            SetValue(SelectecCategoryProperty, value);
            }
            }

            public static readonly DependencyProperty SelectecCategoryProperty =
            DependencyProperty.Register("SelectecCategory", typeof(object), typeof(LookupComboControl));

            In the MainWindow

            Now I get no binding errors. But the original problem still exists... Nothing happens. So here's my question - The UserControl has the DP. Once the MainWindow sets the DP, how does the User Control's VM know about it? Again, I need to somehow get the selected category into the ViewModel. [UPDATE] The source is https://onedrive.live.com/redir?resid=F6FBCF1880A16630!210&authkey=!AA4CNT499dsekPY&ithint=file%2c.zip

            If it's not broken, fix it until it is

            B 1 Reply Last reply
            0
            • K Kevin Marois

              OK, I think you were right. The Output window was showing a Null being passed as SelectedValue. So I made the following changes:: In the control:

              public object SelectecCategory
              {
              get { return (object)GetValue(SelectecCategoryProperty); }
              set
              {
              SetValue(SelectecCategoryProperty, value);
              }
              }

              public static readonly DependencyProperty SelectecCategoryProperty =
              DependencyProperty.Register("SelectecCategory", typeof(object), typeof(LookupComboControl));

              In the MainWindow

              Now I get no binding errors. But the original problem still exists... Nothing happens. So here's my question - The UserControl has the DP. Once the MainWindow sets the DP, how does the User Control's VM know about it? Again, I need to somehow get the selected category into the ViewModel. [UPDATE] The source is https://onedrive.live.com/redir?resid=F6FBCF1880A16630!210&authkey=!AA4CNT499dsekPY&ithint=file%2c.zip

              If it's not broken, fix it until it is

              B Offline
              B Offline
              BubingaMan
              wrote on last edited by
              #6

              Are you still stuck on this? I realise it's a bit late, but anyhow... Assuming your binding is working (didn't test your code, can't at the moment), all you would need to do is add a callback for the dependency property change.

              public static readonly DependencyProperty SelectecCategoryProperty =
              DependencyProperty.Register("SelectecCategory", typeof(object), typeof(LookupComboControl), new UIPropertyMetadata(new PropertyChangedCallback(OnChanged))););

              ...

              private static void OnChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
              {
              //obj is your control. you can cast it here and call a method on it to get rid of the static state you are in
              var control = obj as LookupComboControl;
              control.SetCategoryOnVm();
              }

              private void SetCategoryOnVm()
              {
              vm.Category = this.SelectedCategory //(= your dependency property)
              }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups