Posts filed under 'WPF'

ContentControl messing up with DataContext

              Today I did binding between TreeView.ItemsSource and collection of items to display. I used HierarchicalDataTemplate to describe visualization of the each item in the collection. Inside template, I used ContentControl with was bind to object.Content property of the object (object from collection). What I wanted to do is to store inside tree node reference to object, for future use (handle item selected changed event). Since DataContext is always set by current binding source, I expected to use this for my purpose – to get reference to object from selected FrameworkElement.DataContext.  However, this didn’t worked – inside DataContext of selected item was… object.Content (rather than object itself). After pretty long investigation, I noticed that TextBlock, for example, works as expected. So the problem is with ContentControl, which is assigning DataContext by bind property rather than by source of the property. I found in net this article, which is describing other problem but the same reason, and possible solution.

Add comment April 24, 2008

OMG… Data Templates doesn’t support interfaces!!!

What a frustration.. After hours of failing attempts to find a bug in my code, I discovered: there is no bug! Data templates simply does not support interfaces. This means, if you want to apply data template according to specific interface (“{x:Type yourInterface}”) - forget about this. Not supported. More info about this here.

Add comment April 24, 2008

WPF: Building ContextMenu “On Fly”

         Let’s say we creating application which have to display context menu built “on fly”. By saying “On Fly” I mean we actually have to create context menu object and populate in with items, according to state which is right for mouse click coordinates. This WPF application also hosts Winforms user controls. So we have to support both platforms, and here one possible way to accomplish that:

 

Handiling Winforms:

     Winform controls are hosted inside WindowsFormsHostWPF control. I expected it will fire standard WPF events such as ContextMenuOpening, but it doesn’t. So I subscribed to Winform control’s “MouseUp” and this is how I handling it:

 

ContextMenu cm = new ContextMenu();

//here Context populated by items

 

cm.PlacementTarget = sender as UIElement;

cm.IsOpen = true;

 

Handling WPF: 

That’s all. For WPF window, you can choose 2 possible solutions:

  1. Handle right click as well (this is what I did in my sample).
  2. Handle ContextMenuOpening event. If you choose this path, then instead of setting “IsOpen” property ‘true’, assign “ContextMenu” property by created ContextMenu object. Example:

 

private void OnContextMenuOpening(object sender, ContextMenuEventArgs e)

{

  ContextMenu cm = new ContextMenu();

  //here Context populated by items

 

  FrameworkElementfe = e.Source as FrameworkElement;

  fe.ContextMenu = cm;

}

 

Complete sample get from here.

Add comment April 14, 2008

Enabling keybord input in hosted WPF dialog inside Windows Forms window

     In case you hosting WPF dialog inside Winforms window, and opening it in modaless state – probably your dialog will not receive keyboard input. To fix this issue, call once to this method: EnableModelessKeyboardInterop(). More about it here.

Add comment April 13, 2008

Making disabled buttons with images look grayed (Winforms look).

      By default, when we setting button with image as content to be disabled (IsEnabled = False) the button still looks as it enabled. The simplest solution I found to fix it is to create button style, where on IsEnabled property change I changing opacity of the button. Here is how I did it:

 

The style:

<Window.Resources>

        <Style x:Key=”buttonStyle”TargetType=”{x:Type Button}”>

            <Style.Triggers>

                <Trigger Property=”IsEnabled” Value=”False”>

                    <Setter Property=”Opacity” Value=”0.3″/>

                </Trigger>

            </Style.Triggers>

        </Style>

    </Window.Resources>

 

The button:

<Button Name=”myButton” Style=”{StaticResource buttonStyle}”

 

The beauty of WPF is you can do whatever you want to do with the look of the controls. In my case I wanted disabled button to look as it looks in Winforms, but for any other situation your imagination is the only limit.

 

Point of interest – embedding resources in WPF

Here is how to do that (don’t be confused with logical resources – I talking about resources such as images):

  • Add to project your image (‘Add exist item…’)
  • In properties of image, set Build Action as Content or Resource. Content means your resource will be “loose” – located as is in project directory. Resource means the image will be embedded into the assembly. In my sample, I using embedded image ‘ball.jpg’. If you will look inside runtime folder (…/Debug), you will notice there is no image file, only dlls.

Download sample application from here.

 

Add comment April 13, 2008


Categories

Top Posts

Tags

.NET addin app.config ArrayList bug CAB Configuration ConfigurationManager ConfigurationSection ContentControl ContextMenu CTime; DateTime custom keys DataBinding DataContext Data templates debugging equals gethashcode GUI Hashtable interlocked Invoke lock lock free memcpy MFC multithreading multithreading; lock free override performance SCSF serialization Smart Client Software Factory Styles System.Configuration unsafe virtual functions Visual Studio wait free WinAPI WinForms WinForms\WPF Integration World of Warcraft World of Warcraft; Addon

Archives