TabItem in a separate XAML

If what you want to do is simply make the code more manageable then I would recommend defining each tab’s data in a user control, but still have the TabItem in the main tab control. Let’s assume that your original code was this: <TabControl> <TabItem Header=”Tab 1″> <Grid> <TextBlock Text=”Tab Data” /> </Grid> </TabItem> </TabControl> … Read more

WPF + MVVM + RadioButton : How to handle binding with single property?

You need a IValueConverter. //define this in the Window’s Resources section or something similiarly suitable <local:GenderConverter x:Key=”genderConverterKey” /> <RadioButton Content=”M” IsChecked=”{Binding Gender, Converter={StaticResource ResourceKey=genderConverterKey}, ConverterParameter=M}” /> <RadioButton Content=”F” IsChecked=”{Binding Gender, Converter={StaticResource ResourceKey=genderConverterKey}, ConverterParameter=F}” /> The converter public class GenderConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return ((string)parameter … Read more

WPF ItemsControl the current ListItem Index in the ItemsSource

I asked the same thing a while ago here There isn’t a built in Index property, but you can set the AlternationCount of your ItemsControl to something higher than your item count, and bind to the AlternationIndex <TextBlock Text=”{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Mode=TemplatedParent}, FallbackValue=FAIL, StringFormat={}Index is {0}}” /> It should be noted that this solution may … Read more

TreeView, HierarchicalDataTemplate and recursive Data

You should only have to declare the HierarchicalDataTemplate for NodeViewModel as this is the only thing showing in the TreeView, and bind the actual ItemSource to the TreeView <TreeView ItemsSource=”{Binding Items}”> <TreeView.Resources> <HierarchicalDataTemplate DataType=”{x:Type local:NodeViewModel}” ItemsSource=”{Binding Children}”> <TextBlock Text=”{Binding Name}”></TextBlock> </HierarchicalDataTemplate> </TreeView.Resources> </TreeView> Full Example Xaml: <Window x:Class=”WpfApplication13.MainWindow” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:local=”clr-namespace:WpfApplication13″ Title=”MainWindow” x:Name=”UI” Width=”343″ Height=”744.625″ … Read more

How to Print Preview when using a DocumentPaginator to print?

So I got it working after reading Pro WPF in C# 2008 (Page 726). Basically the DocumentViewer class needs an XPS file to present a print preview of it. So I do the following: PrintDialog dialog = new PrintDialog(); var paginator = new RowPaginator(rowsToPrint) { PageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight) }; string tempFileName = System.IO.Path.GetTempFileName(); … Read more

WPF IsEditable=true ComboBox filled with objects displays the ToString() as the selected item

You can do this entirely within Xaml <ComboBox IsTextSearchEnabled=”True” IsEditable=”True” ItemsSource=”{Binding MyObjectCollection}” TextSearch.TextPath=”MyObjectName”> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text=”{Binding MyObjectName}” /> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> The upside is that you can define and change this however you want in your XAML without any code-behind. You bind the ItemsSource to your collection of objects, and then you set the … Read more