Binding as a Resource

Direct answer to your question is “yes, you can define a binding as a resource”. The problem here is how do you then make any use of it? One possibility is to create an extension class which would pull the binding from the resources and apply it: public class BindingResourceExtension : StaticResourceExtension { public BindingResourceExtension() … Read more

TextBox TextTrimming

Try a style like this (I’ve added background colours to make the change obvious): <Style TargetType=”TextBox”> <Setter Property=”Background” Value=”Yellow” /> <Style.Triggers> <DataTrigger Binding=”{Binding IsKeyboardFocused, RelativeSource={RelativeSource Self}}” Value=”false”> <Setter Property=”Template”> <Setter.Value> <ControlTemplate TargetType=”TextBox”> <TextBlock Text=”{TemplateBinding Text}” TextTrimming=”CharacterEllipsis” Background=”Red” /> </ControlTemplate> </Setter.Value> </Setter> </DataTrigger> </Style.Triggers> </Style>

Different views / data template based on member variable

I usually use a ContentControl to display the data, and swap out the ContentTemplate in a trigger based on the property that changes. Here’s an example I have posted on my blog that swaps a template based on a bound property <DataTemplate x:Key=”PersonTemplate” DataType=”{x:Type local:ConsumerViewModel}”> <TextBlock Text=”I’m a Person” /> </DataTemplate> <DataTemplate x:Key=”BusinessTemplate” DataType=”{x:Type local:ConsumerViewModel}”> … Read more

How to bind a TextBlock to a resource containing formatted text?

Here is my modified code for recursively format text. It handles Bold, Italic, Underline and LineBreak but can easily be extended to support more (modify the switch statement). public static class MyBehavior { public static string GetFormattedText(DependencyObject obj) { return (string)obj.GetValue(FormattedTextProperty); } public static void SetFormattedText(DependencyObject obj, string value) { obj.SetValue(FormattedTextProperty, value); } public static … Read more

WPF, ‘Object reference not set to an instance of an object’ in Designer

What Alex says is the way to go. But I think its a little confusing to understand what he is saying. Assuming you have your project open in Visual Studio, open another Visual Studio instance and select Debug->Attach To Process. In the dialog which opens select XDesProc.exe (which is the XAML UI Designer) for VS2012 … Read more

Binding in WPF to element of array specified by property

Another alternative is to use MultiBinding with a converter: <Window x:Class=”WpfApplication1.MainWindow” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:local=”clr-namespace:WpfApplication1″ Title=”MainWindow” Height=”350″ Width=”525″> <StackPanel Orientation=”Vertical”> <StackPanel.Resources> <local:FoodIndexConverter x:Key=”foodIndexConverter” /> </StackPanel.Resources> <TextBlock Text=”{Binding DessertIndex}” /> <TextBlock Text=”{Binding Food[2]}” /> <TextBlock> <TextBlock.Text> <MultiBinding Converter=”{StaticResource foodIndexConverter}”> <Binding Path=”DessertIndex” /> <Binding Path=”Food”/> </MultiBinding> </TextBlock.Text> </TextBlock> </StackPanel> </Window> Then in the code-behind, the converter is defined … Read more

How to Stretch WPF Tab Item Headers to Parent Control Width

I took Jordan’s example and made some changes to it. This version should work for any number of tabs: namespace WpfApplication1.Converters { public class TabSizeConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { TabControl tabControl = values[0] as TabControl; double width = tabControl.ActualWidth / tabControl.Items.Count; //Subtract 1, otherwise we … Read more

What does “{x:Static}” mean in XAML?

It is a way to insert any static value into XAML. For example, if I have a class: namespace A { public class MyConstants { public static readonly string SomeConstantString = “BAM!”; } } I can place it into a WPF UI using XAML like this: <TextBlock Text=”{x:Static A:MyConstants.SomeConstantString}” /> Notice, you will have to … Read more

WPF Fade Animation

I don’t know how to do both animations (fade in and fade out) in pure XAML. But simple fade out can be achieved relatively simple. Replace DataTriggers with Triggers, and remove ExitActions since they makes no sense in Fade out scenario. This is what you will have: <Style TargetType=”FrameworkElement” x:Key=”animatedList”> <Setter Property=”Visibility” Value=”Hidden”/> <Style.Triggers> <Trigger … Read more

Cannot find source for binding with reference ‘RelativeSource FindAncestor’ [duplicate]

DataGridTemplateColumn is not part of the visual or logical tree, and therefore has no binding ancestor (or any ancestor) so the RelativeSource doesn’t work. Instead you have to give the binding the source explicitly. <UserControl.Resources> <local:BindingProxy x:Key=”proxy” Data=”{Binding}” /> </UserControl.Resources> <DataGridTemplateColumn Visibility=”{Binding Data.IsVisible, Source={StaticResource proxy}, Converter={StaticResource BooleanToVisibilityConverter}}”> And the binding proxy. public class BindingProxy : … Read more

tech