Monday, April 13, 2009

Trips&Ticks: Do you know how can you obtain access to nested types through XAML?

Download source files - here.

XAML is a declarative XML-based language created by Microsoft which is used to initialize structured values and objects. I have already written how we can obtain access to properties of instance that located in other properties here (I have described binding's potential). But what can we do if we need to access to nested type? Which syntax should me use? It is easy: "+" indicates nested type in XAML:

local:MultiLevelButton+NestedInfo.NestedName

A lot of code snippets you can see in sources.

Tuesday, March 3, 2009

OnTime desktop light client.

Download source files - here.
Download binary files - here.

My previous customer uses OnTime as bts. It is very slowly tool. I hated it! I tired to wait any respond from this app. I have created a little and more fast tool for self using. This tool ins't complete (I have stopped to work with this customer and I can't have an access to bd for continue implementation).
Next issues need a fix:
  • SQL exception isn't handled infrequent :)
  • Icons aren't useful. Useful tooltips correct this defect ;)
  • Attachment download works in some thread that GUI (It is shameful, but I haven't had time, really)
  • App works readonly.
  • Sort and Search hasn't implemented.
  • We can work with self issues only.
Advantage:
  • It is really more fast tool than official.
  • Persist state doesn't store in bd. It saves in current PC only.
Axosoft releases new version (9.0). It is more fast than previous (8.0). But it is still no enough.
If you want to implement some features or to fix bugs you need to know that for correct run sources you need to set connection string in the next files:
  1. app.config
  2. Settings.Designer.cs
  3. OTData.dbml
  4. and Settings.settings in two points.
I have changed my connect string to "your_connecting_string" for more handy replace ;).

Tuesday, February 3, 2009

Trips&Ticks: Do you know how you can extract various instances of one object out of resources in WPF?


Download source files - here


I often use a style&template that is magnificent possibility of WPF. It allows to define one style out of resources to many elements. I can easily define instance of some object in resources and extract it there where I require. But it has one limitation:
For example for following:

XAML:
<Button x:Key="ButtonPrototype" />

C#:
Resources["ButtonPrototype"];
- returns always one instance (like Singlton Pattern). But what can I make if I require more than one such object? It isn't problem as XAML is very flexible and x:Shared attribute eliminates this trouble. Following code will extract various instances:

XAML:
<Button x:Key="ButtonPrototype" x:Shared="False" />

C#
Resources["ButtonPrototype"]


PS: In my opinion last realization shares traits with Prototype Pattern.

Saturday, January 10, 2009

Bind to many method simultaneously. Binding in WPF part 2.

Download source files - here
Download binary files - here

Target:
We have some views that have as source one shared collection. They are located in any ItemsControls (ListBoxs in my case). Those views pass form different parametrized methods (we use the ObjectDataProvider and bind to a method possibility for this purposes). How can we implement one control element's property for affect on those views? We have restricts - it is needed to make in XAML code.

Problem:
How can we pass a value of control element as parameter in bind to a methods?

Solution:
First of all we should declare some (I used "two", so I will write in next time "two" instead of "some") ObjectDataProvider objects.

<ObjectDataProvider ObjectType="{x:Type local:CItems}"
MethodName="GetMax" x:Key="max" >
<ObjectDataProvider.MethodParameters>
<system:Int32>3</system:Int32>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

<ObjectDataProvider ObjectType="{x:Type local:CItems}"
MethodName="GetMin" x:Key="min" >
<ObjectDataProvider.MethodParameters>
<system:Int32>3</system:Int32>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

Bind those objects to views:

<ListBox ItemsSource="{Binding Source={StaticResource min}}" />
<ListBox ItemsSource="{Binding Source={StaticResource max}}" />

Now we need to create a binding to control element's property that has affect to both those data provider. We use MultiBinding with Mode is set OneWayToSource for this purpose. We should implement Converter of MultiBinding. Since we set Mode as OneWayToSource we need only ConvertBack method:

public Object[] ConvertBack( Object value, Type[] targetTypes, Object parameter, CultureInfo culture )
{
Int32 cnt = targetTypes.Length;
Object[] result = new Object[ cnt ];
String str = value.ToString();

if( !String.IsNullOrEmpty( str ) )
{
Int32 index = Int32.Parse( value.ToString() );

for( Int32 i = 0; i < cnt; ++i )
{
result[ i ] = index;
}
}

return result;
}

Note: We should unbox value to Int32 other case value isn't correctly passed to method of data provider.

Now we can easy implement this MultiBinding our XAML code:

<ComboBox.Text>
<MultiBinding Mode="OneWayToSource" Converter="{StaticResource artfull}">
<Binding Source="{StaticResource max}"
Path="MethodParameters[0]"
BindsDirectlyToSource="true"
UpdateSourceTrigger="PropertyChanged"
/>
<Binding Source="{StaticResource min}"
Path="MethodParameters[0]"
BindsDirectlyToSource="true"
UpdateSourceTrigger="PropertyChanged"
/>
</MultiBinding>
</ComboBox.Text>

Now when Text property of ComboBox is changed that views will be updated. Easy and nice :). If some details isn't clear - source and binary files were attached in the beginning of article.

Saturday, December 6, 2008

Trips&Ticks: Do you know how you can use GroupName property of RadioButton for ToggleButton in WPF?


We can use GroupName property for mutually exclusive RadioButton but what can we do if we want to use mutually exclusive ToggleButton? ToggleButton doesn't have like ability. Please, do look in RadioButton what can you see? Exactly, RadioButton is inherit from ToggleButton so we can set in Template property ControlTemplate of ToggleButton. So our RadioButtons will be look like ToggleButtons besides will have GroupName property.

Friday, November 21, 2008

Trips&Ticks: Do you know how you can cast Object to AnonymousType?

I experimented with LINQ in my private project and question that was showed in title took me. I had next situation:

I set list in one method:

private void SetList()
{
SomeList.ItemsSource = ( from someTable in data.SomeTable
select new
{
someTable.Id,
someTable.Name,
Priority = someTable.Priority,
} );
In other method I want retrieve Id of selected item. How can I do it? I have found interesting solution in blog of Tomas Petricek.
private Int32 GetID()
{
Object selectedItem = DefectList.SelectedItem;

if( null != selectedItem )
{
var item = Utility.Cast( selectedItem, new
{
Id = 0,
Name = String.Empty,
Priority = String.Empty,
} );

Int32 id = item.Id;
}
}
public static T Cast( object obj, T type )
{
return (T)obj;
}

PS: As for me the best way is:
  • implement class with three like property.
  • create collection with items of this class.
  • set in ItemsSource new created collection.
  • now we can easy case Object to this class.

Tuesday, November 18, 2008

Such different paths. Binding in WPF part 1.


Download source files - here

We pleasure to using Binding in WPF. But all ways of using have we known? Only direct binds to dependency property like next code?

<TextBlock Text="{Binding Path=Height, ElementName=myButton}">

This is poor. We can bind to property of value's property. How?
Easy:

<TextBlock Text="{Binding Path=Background.(SolidColorBrush.Opacity), ElementName=myButton}"/>

and

<TextBlock Text="{Binding Path=(Button.Content).(TextBlock.FontSize), ElementName=myButton}"/>

In attached sample I show how we can use this for EventTriggers.