Wednesday, May 26, 2010

SelectedItem disables associated Buttons

0 comments

image

Situation: There is a List, and some buttons that are ought to act on current selected item in the list. But if there are no items or no item is selected then you want these buttons to be disabled.

The idea: Having buttons disabled/enabled implicitly/automatically based on the currently selected item in the list.

Style:
  1. <Style x:Key="disableIfNoListItemSelected" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
  2.   <Style.Setters>
  3.     <Setter Property="UIElement.IsEnabled" Value="True" />
  4.   </Style.Setters>
  5.   <Style.Triggers>
  6.     <DataTrigger Binding="{Binding SelectedIndex}" Value="-1">
  7.       <Setter Property="UIElement.IsEnabled" Value="False" />
  8.     </DataTrigger>
  9.   </Style.Triggers>
  10. </Style>

 

Usage:
  1. <Button Name="cmdShow"
  2.                     DataContext="{Binding ElementName=grdTemplates}"
  3.                     Style="{StaticResource disableIfNoListItemSelected}">
  4.                 </Button>

Notice in the usage, when applying a style to a button, it is required to assign a DataContext of this button to the List that is being watched.

Tuesday, May 18, 2010

Closing Modal Dialog Window in WPF

1 comments

If it is a modal dialog box, then it most probably has OK, Cancel buttons. If it has OK, Cancel buttons, them most probably the “client initiator” wants to know if Ok was pressed or not.

image

Remember WinForms had DialogResult enumeration? It is a bit different with WPF, ShowDialog() returns bool?.

bool? result = form.ShowDialog();if (result.HasValue && result.Value){ DoMyThing();}

When will result.Value be false?  Make sure that you do this only for True/Ok case, the default is false:

private void Ok_Click(..){ this.Close(); this.DialogResult = true;}

and one more thing. To close dialog on Esc, you want this:

image

For OK button you want this:

image

This way, when user presses Enter, OK button is pressed.

 

Warning about IsCancel and Cancel button. There seems no way to Cancel the “Cancel event”. So if user clicks Cancel, windows is going to be closed. If you know a way for canceling the closure, please post.

Wednesday, May 12, 2010

Implicitly “degrading” ui controls

0 comments

If this is valuable then mostly as an idea for now. Current implementation might be too limited and can be further enhanced.

The goal is to hide or disable ui controls if they do not show anything, or whatever they show is missing. Degrades=Hide or Disable.

In WPF terms and in this cut, I’ve made that ui control becomes hidden or disabled automatically if when DataContext becomes null. This implies that host has to be bound to something real, and presence of that real thing implicitly degrades(or not) the host.

Styles from Resources
  1. <Style x:Key="hideStackPanelIfNull" TargetType="StackPanel" BasedOn="{StaticResource {x:Type StackPanel}}">
  2.     <Style.Setters>
  3.       <Setter Property="UIElement.Visibility" Value="{x:Static Visibility.Visible}" />
  4.     </Style.Setters>
  5.     <Style.Triggers>
  6.       <DataTrigger Binding="{Binding}" Value="{x:Null}">
  7.         <Setter Property="UIElement.Visibility" Value="{x:Static Visibility.Hidden}" />
  8.       </DataTrigger>
  9.     </Style.Triggers>
  10.   </Style>
  11.   
  12.   <Style x:Key="disableButtonIfNull" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
  13.     <Style.Setters>
  14.       <Setter Property="UIElement.IsEnabled" Value="True" />
  15.     </Style.Setters>
  16.     <Style.Triggers>
  17.       <DataTrigger Binding="{Binding}" Value="{x:Null}">
  18.         <Setter Property="UIElement.IsEnabled" Value="False" />
  19.       </DataTrigger>
  20.     </Style.Triggers>
  21.   </Style>
  22.   
  23.   <Style x:Key="disableStackPanelIfNull" TargetType="StackPanel" BasedOn="{StaticResource {x:Type StackPanel}}">
  24.     <Style.Setters>
  25.       <Setter Property="UIElement.IsEnabled" Value="True" />
  26.     </Style.Setters>
  27.     <Style.Triggers>
  28.       <DataTrigger Binding="{Binding}" Value="{x:Null}">
  29.         <Setter Property="UIElement.IsEnabled" Value="False" />
  30.       </DataTrigger>
  31.     </Style.Triggers>
  32.   </Style>

Usage Example
  1. <StackPanel DataContext="{Binding ElementName=myList, Path=SelectedItem}"
  2.           VerticalAlignment="Center" Orientation="Horizontal"
  3.           Style="{StaticResource hideStackPanelIfNull}">
  4.     <TextBlock Text="Name:"></TextBlock>
  5.     <TextBlock Text="{Binding Path=Name}"></TextBlock>
  6. </StackPanel>

 

In this example StackPanel is bound to SelectedItem. If there is nothing in the list, SelectedItem becomes null and that degrades the controls that bear presented here styles, textblocks become disabled or hidden.

Some caveat. Obviously this requires to overwrite your style. It might conflict with your style requirements. In above shown example I am pointing to StackPanels which usually do not need style.

Converter

0 comments

WPF binding binds one thing to another based on default conversion. For fancier conversion there is Converter. With Converter you can map more than one things to one thing.

User List in a Combo Box
  1.     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  2.     {
  3.         string name = string.Empty;
  4.         if (value is MyUser)
  5.         {
  6.             MyUser user = value as MyUser ;
  7.             name = string.Format("{0}{1}[{2}]", user.FirstName, user.LastName, user.ZipCode.ToString());
  8.         }
  9.         return name;
  10.     }… …<local:UserNameConverter x:Key="userNameConverter"/>
  11. <ComboBox>
  12.   <ComboBox.ItemTemplate>
  13.     <DataTemplate>
  14.       <StackPanel Orientation="Horizontal">
  15.         <TextBlock Text="{Binding Converter={StaticResource userNameConverter}}" />
  16.       </StackPanel>
  17.     </DataTemplate>
  18.   </ComboBox.ItemTemplate>
  19. </ComboBox>

 

 

Some Advanced Converters on Codeplex.com is here.

Wednesday, May 5, 2010

Troubles with VS2008 WPF Designer Crushes

0 comments

Event log:

.NET Runtime version 2.0.50727.3603 - Fatal Execution Engine Error (7A2E10D2) (0)

EventType clr20r3, P1 devenv.exe, P2 9.0.30729.1, P3 488f2b50, P4 NIL, P5 NIL, P6 NIL, P7 NIL, P8 NIL, P9 NIL, P10 NIL.

Faulting application devenv.exe, version 9.0.30729.1, stamp 488f2b50, faulting module kernel32.dll, version 5.1.2600.5781, stamp 49c4f482, debug? 0, fault address 0x00012afb.

Some relevant but not useful suggestions:

1, 2, 3, 4,  any other ones ?

Tuesday, May 4, 2010

Telerik vs. Xceed

0 comments

 image

See above for a bit of syntax. Preatty similar structure. But Telerik has TextAlignment, daaa, Xceed does not. 1:0.

image

On the left. Telerik lets you specify “Auto” or prioritize Column Widths, just like with standard Grid’s Rows/Columns. Xceed does not have this. Xceed will resize the column, but twice more space than needed, useless. 2:0, Telerik is winning.

 

One more thing, Xceed demands uniqueness for FieldNames. This limits columns that would rather accept “.” binding of item itself and bind deeper inside the template. This is not good, Telerik scores one more. to sum up: 3:0, Telerik wins in this context.

As you can see, this comparison is based on basic features.

Sunday, May 2, 2010

Pyramid

0 comments

If you would try to visualize an architecture of the product, how would it look like ?

image

One of the course idea for architecture is an awareness of layers, one on another, looks like Pyramid. Each layer references next one. Layers do not jump over each other. Keeping a notion of Parent->Child relationship or Client-Server or Master-Slave(this term is not used anymore).

For every class there is a client, so Class is a “server” and most probably it references another one. So that class is a “server” and a client for another server.

so per this concept.

rule 1: parent can reference child and not the child’s child

rule 2: child does not tell parent what to do, maximum it tells parent what has happened in means of return values or events. Parent will decide on next actions.

Saturday, May 1, 2010

AssemblyDescription, marking with build type

0 comments

image

 

It might a be a useful idea to mark your assemblies for Debug or Release modes. So that when looking in Windows Explorer, you could determine right away, what kind of dll/exe is this.

 

 

#if DEBUG
[assembly: AssemblyDescription("AS2-Debug")]
#else
[assembly: AssemblyDescription("AS2-Release")]
#endif

 

image