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
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