Author Topic: WPF Combobox Binding  (Read 2809 times)

0 Members and 1 Guest are viewing this topic.

djee

  • Newt
  • Posts: 49
WPF Combobox Binding
« on: March 21, 2016, 05:10:09 PM »
I'm trying to update the comboBox content in my palette based on the AutoCAD variable "LUNITS". So whenever the "LUNITS" change my combobox content also change... Also, it would need to update/check the AutoCAD variable "LUNITS" whenever the user change the current drawing... Any suggestion on how to achieve this? Thanks!

n.yuan

  • Bull Frog
  • Posts: 348
Re: WPF Combobox Binding
« Reply #1 on: March 22, 2016, 09:44:22 AM »
Well, you obviously need to have a ViewModel that implements INotifyPropertyChanged interface and expose a property that has the value of "LUNITS". Since you want to use a COMBOBOX, that means you expect the LUNIT value to be changed in a list of pre-determined value. So, the ViewModel may also provide a list of LUNIT values (type of "Double") as the ComboBox's "ItemsSource", or you can hard-code the ComboBox's item list in XAML, if the pre-determined list does not change at all.

Then, in your ViewModel, you may need to handle AutoCAD's SystemVariableChanged event to monitor if LUNIT is changed or not, and when LUNIT changes, the ViewModel's property that is bound to the ComboBox's SelectedValue/Item would set and trigger's OnPropertyChange in the ViewModel, so that the UI is notified and the ComboBox gets updated automatically.

djee

  • Newt
  • Posts: 49
Re: WPF Combobox Binding
« Reply #2 on: March 22, 2016, 11:20:50 AM »
Based on the "LUNITS" variable (imperial or metric), my combobox let the user select a set of predefine metric or imperial text sizes... So if "LUNITS" is metric, my combobox should display a choice of metric text sizes (2.0mm, 2.5mm, 3.0mm, 5.0mm). If "LUNITS" is imperial, my ComboBox should display a choice of imperial text sizes (5/64, 3/32, 1/8, 9/64, 13/64). I have to look deeply into MVVM pattern... Any suggestion (for a newby) on how to
Quote
have a ViewModel that implements INotifyPropertyChanged interface and expose a property that has the value of "LUNITS"
Thank you very much n.yuan, as always you are of great help!

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: WPF Combobox Binding
« Reply #3 on: March 22, 2016, 12:37:10 PM »
Hi,

You can see a WPF / MVVM example using combo boxes here (a C#6 version there).
Speaking English as a French Frog

djee

  • Newt
  • Posts: 49
Re: WPF Combobox Binding
« Reply #4 on: March 22, 2016, 03:17:02 PM »
Ok... :woow: I'm going to study this very carefully... Thanks gile! BTW, I had to change my reference so that AcCoreMgd was included in the ConversionPalette_18 project... Since I'm debugging in 2016, I had to change the framework to 4.5(...). This is ok right? Thanks again!
« Last Edit: March 22, 2016, 03:29:07 PM by djee »

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: WPF Combobox Binding
« Reply #5 on: March 22, 2016, 03:39:04 PM »
No, the ConversionPalette_18 project targets AutoCAD 2012. It's here to show how to share the same code between two different pojects (referencing different libraries).

If you do not care about AutoCAD 2012, you can simply remove the ConversionPalette_18 project from the solution.
Then you can change the referenced libraties according to the AutoCAD version you're targeting.
Speaking English as a French Frog

djee

  • Newt
  • Posts: 49
Re: WPF Combobox Binding
« Reply #6 on: March 22, 2016, 03:52:43 PM »
Thanks for the clarification! I'm now looking at the associated PDF file you've created on http://cadxp.com/topic/42380-conversionpalette/ . Very nice indeed!

djee

  • Newt
  • Posts: 49
Re: WPF Combobox Binding
« Reply #7 on: March 30, 2016, 12:13:15 PM »
In your opinion, do I have to look at implementing the "ICommand" interface? :crazy2:

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: WPF Combobox Binding
« Reply #8 on: March 30, 2016, 12:27:16 PM »
It depends on what you want to do.
You need to implement ICommand if you want to bind a control (typically a button) to an action.
Speaking English as a French Frog

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: WPF Combobox Binding
« Reply #9 on: March 31, 2016, 07:31:28 AM »
If you're going MVVM then implementing ICommand is a necessity.  Anyone beginning WPF/MVVM should create a RelayCommand class and stick it on your tool bar or put it in a folder to use on every project that's using WPF
Revit 2019, AMEP 2019 64bit Win 10