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

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

WPF set border background in trigger

Common mistake. You have set the Border.Background property directly which will always override the value set by your trigger. (Locally set values have a very high precedence, style has a pretty low precedence.) Instead, you should move your “normal” background into the Style like so: <Border> <Border.Style> <Style TargetType=”Border”> <Setter Property=”Background”> <Setter.Value> <LinearGradientBrush> <LinearGradientBrush.GradientStops> <GradientStop … Read more

WPF – How to create image button with template

You won’t need dependency properties because you are inheriting from Button. You already have the IsPressed and IsEnabled properties. In fact, this is all you need: <Button x:Class=”TestWpfApplication.ThreeStateImageButton” xmlns=”” xmlns:x=””> <Button.Template> <ControlTemplate TargetType=”{x:Type Button}”> <Grid> <Image Name=”Normal” Source=”Resources/Normal.png”/> <Image Name=”Pressed” Source=”Resources/Pressed.png” Visibility=”Hidden”/> <Image Name=”Disabled” Source=”Resources/Disabled.png” Visibility=”Hidden”/> </Grid> <ControlTemplate.Triggers> <Trigger Property=”IsPressed” Value=”True”> <Setter TargetName=”Normal” Property=”Visibility” Value=”Hidden”/> … Read more

How can I tell my DataTemplate to bind to a property in the PARENT ViewModel?

The answer is this: <DataTemplate x:Key=”CodeGenerationMenuTemplate”> <MenuItem Header=”{Binding Title}” Command=”{Binding DataContext.SwitchPageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}” CommandParameter=”{Binding Title}”/> </DataTemplate> I just saw that Nir had given me the syntax to solve the above issue on this question: What is the best way in MVVM to build a menu that displays various pages?.

How to bind DataTemplate datatype to interface?

You can bind to interfaces by telling wpf explicitly that you are binding to an interface field: (Please note that ViewModelBase is simply a base-class that implements the INotifyPropertyChanged interface) public class Implementation : ViewModelBase, IInterface { private string textField; public string TextField { get { return textField; } set { if (value == textField) … Read more

How to get TreeViewItem from HierarchicalDataTemplate item?

TreeViewItem item = (TreeViewItem)(mainTreeList .ItemContainerGenerator .ContainerFromIndex(mainTreeList.Items.CurrentPosition)); DOES NOT WORK (for me) as mainTreeList.Items.CurrentPosition in a treeview using a HierarchicalDataTemplate will always be -1. NEITHER DOES below as as mainTreeList.Items.CurrentItem in a treeview using a HierarchicalDataTemplate will always be null. TreeViewItem item = (TreeViewItem)mainTreeList .ItemContainerGenerator .ContainerFromItem(mainTreeList.Items.CurrentItem); INSTEAD I had to set a the last selected TreeViewItem … Read more

How do I dynamically generate columns in a WPF DataGrid?

Ultimately I needed to do two things: Generate the columns manually from the list of properties returned by the query Set up a DataBinding object After that the built-in data binding kicked in and worked fine and didn’t seem to have any issue getting the property values out of the ExpandoObject. <DataGrid AutoGenerateColumns=”False” ItemsSource=”{Binding Results}” … Read more