Set value to null in WPF binding

I am using .NET 3.5 SP1 so it’s very simple: <TextBox Text=”{Binding Price, TargetNullValue=””}”/> Which stands for (thanks Gregor for your comment): <TextBox Text=”{Binding Price, TargetNullValue={x:Static sys:String.Empty}}”/> sys is the imported xml namespace for System in mscorlib: xmlns:sys=”clr-namespace:System;assembly=mscorlib” Hope that helped.

How to get StackPanel’s children to fill maximum space downward?

It sounds like you want a StackPanel where the final element uses up all the remaining space. But why not use a DockPanel? Decorate the other elements in the DockPanel with DockPanel.Dock=”Top”, and then your help control can fill the remaining space. XAML: <DockPanel Width=”200″ Height=”200″ Background=”PowderBlue”> <TextBlock DockPanel.Dock=”Top”>Something</TextBlock> <TextBlock DockPanel.Dock=”Top”>Something else</TextBlock> <DockPanel HorizontalAlignment=”Stretch” VerticalAlignment=”Stretch” … Read more

What is it about DataTable Column Names with dots that makes them unsuitable for WPF’s DataGrid control?

You can keep AutoGenerateColumns set to true and add an eventhandler to deal with any periods (or other special characters): <DataGrid Name=”r2″ ItemsSource=”{Binding Path=.}” AutoGeneratingColumn=”r2_AutoGeneratingColumn”> </DataGrid> Codebehind: private void r2_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.PropertyName.Contains(‘.’) && e.Column is DataGridBoundColumn) { DataGridBoundColumn dataGridBoundColumn = e.Column as DataGridBoundColumn; dataGridBoundColumn.Binding = new Binding(“[” + e.PropertyName + “]”); … Read more

Get a bitmap image from a Control view

I wouldn’t call it easy…but the key component is the RenderTargetBitmap, which you can use as follows: RenderTargetBitmap rtb = new RenderTargetBitmap((int)control.ActualWidth, (int)control.ActualHeight, 96, 96, PixelFormats.Pbgra32); rtb.Render(control); Well, that part is easy, now the RTB has the pixels stored internally…but your next step would be putting that in a useful format to place it on … Read more

Make WPF Image load async

I suggest you to use a Binding on your imgGravatar from XAML. Set IsAsync=true on it and WPF will automatically utilize a thread from the thread pool to pull your image. You could encapsulate the resolving logic into an IValueConverter and simply bind the email as Source in XAML: <Window.Resouces> <local:ImgConverter x:Key=”imgConverter” /> </Window.Resource> … … Read more

WPF datagrid header text binding

This is the easy way to bind the DataGridTextColumn header to the data context: <DataGrid x:Name=”summaryGrid” Grid.Row=”3″ AutoGenerateColumns=”False” IsReadOnly=”True” CanUserAddRows=”False”> <DataGrid.Columns> <DataGridTextColumn Header=”Hard Coded Title” Width=”*”/> <DataGridTextColumn Width=”100″> <DataGridTextColumn.Header> <TextBlock Text=”{Binding DataContext.SecondColumnTitle, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}”/> </DataGridTextColumn.Header> </DataGridTextColumn> <DataGridTextColumn Width=”150″> <DataGridTextColumn.Header> <TextBlock Text=”{Binding DataContext.ThirdColumnTitle, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}”/> </DataGridTextColumn.Header> </DataGridTextColumn> </DataGrid.Columns> </DataGrid> You will obviously need to … Read more

ItemsControl with multiple DataTemplates for a viewmodel

You can create multiple ObservableCollections and then bind your ItemsSource to a CompositeCollection which joins those collections. Then in your XAML you can create different DataTemplates for the respective types using the DataType property which like styles gets automatically applied if it is placed in the resources. (You can also create the composite in XAML … Read more