Showing posts with label wpf. Show all posts
Showing posts with label wpf. Show all posts

Tuesday, November 1, 2011

WPF DataGrid Combo Box Databinding

I'm using WPF and MVVM.

I have my View and my ViewModel.

My ViewModel has models of its own.

These models are used to communicate and translate data from a database.

I want my models to be POCOs and not to have more information than each should, for instance, I don't want my POCO model to contain a list of another item just so it knows which one it needs to reference. (This will make sense soon)

I have a PlateConfiguration that references PlateLocations and PlateTypes. I do not want my PlateConfiguration to contain a list of all PateLocations and PlateTypes, just the referenced PlateLocation and PlateType.

To accomplist this through databinding, you need to reference an ancestor and the DataContext of that ancestor.

Here is how:

Value="{Binding Path=DataContext.PlateTypes, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"

I am referencing the list of PlateTypes on my ViewModel within an element that has a different context.

The context of my element is:

ItemsSource="{Binding Configurations}"

This changes my context within the element. So, if I try and reference PlateTypes through binding, the element cannot.

Also, I want the selected PlateType to be set on the actual Database Model and not on the ViewModel.

I want the Displayed Value to be the Abbreviation Value that is set by the user.

To display the text of the desired value:

DisplayMemberPath="PlateTypeAbbrev"

This references the Configurations DataContext and not the ViewModel.

The entire code is:



Also,

I've changed the way I bind to my data. It seems more intuitive, but a problem arose, when I tried to change the values. You need to add "Mode=TwoWay, UpdateSourceTrigger=PropertyChanged"
to your binding. If you notice, I have changed the type of column in the DataGrid.

Wednesday, February 16, 2011

Focus & WPF

Sometimes, I would like to take the focus off an element.

I don't necessarily want to put the focus on another element.

In an instance, where the element is within a "StackPanel", you can do:

// XAML
< stackpanel focusmanager.isfocusscope="True" name="MyStackPanel" > ...

// C#

FocusManager.SetFocusedElement(MyStackPanel, null);

This will not focus on any element, and as importantly, remove focus on any element that has focus in the StackPanel.

Wednesday, January 26, 2011

WPF Hosting Winform

When hosting a WinForm in a WPF application, the WinForm will not render correctly.

This is because in the Program.cs file:

        System.Windows.Forms.Application.EnableVisualStyles();

is called.

You must do this manually.

{
   System.Windows.Forms.Application.EnableVisualStyles();

   WindowsFormsHost wfh ...

   ...
}

Friday, January 21, 2011

C# WPF Grid Mouse Event Not Firing

A UIELEMENT in WPF that inherits from Panel, must have a BACKGROUND color in order to bubble a "Click' event.

Thursday, September 23, 2010

C# WPF Grid System

There are multiple "root" elements that you can use when designing a WPF application.

I want to talk about the "Grid" element.

This is pretty useful when you have a 'template' layout.  When you know where UI elements are going to be, and/or you are sure that your screen will be resized, you can use the 'Grid' element as your root.

The Grid element is very similar to the Table element in HTML.

A specific question I came across, was "Can I make one row a fixed size, while the other rows grow as the user changes the size of the form?".  This was an issue, because as the user changes the size of the form, the Grid cells, grow and shrink with the form.

Yes, you can set a specific row's height or a column's width.

There are 3 different ways you can set the height of a row.

row.Height = new GridLength(...

The three choices are:

    Pixel - fixed.  The value entered will be the amount of pixels exactly.
    Auto - this will make the row the size needed by the content.
    Star - takes as much as needed, or the amount in percent.

Just thought this was interesting.

Thursday, September 2, 2010

No applicable name scope exists to resolve the name, Error. C# WPF

This error can occur when:

      A storyboard begins ->  myStoryBoard.Begin(myElement); <- and a programatically created element has not been added to a parent yet.

Add 'myElement' to a parent on the form.

Tuesday, August 24, 2010

C# WPF Button Blink Problem

In a WPF application, if a button is clicked, the focus gets put on that button.

Sometimes this will cause the button to constantly blink.

In order to stop the button from blinking, some solutions say to put the 'focus' onto another button.

I think, a cleaner way, is to set the 'Focusable' property to false.  This doesn't seem as 'hacky' as setting focus to another button, and still fixes the problem.