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

Xamly determine if a ListBox.Items.Count > 0

The ListBox contains a HasItems property you can bind to. So you can just do this: <BooleanToVisibilityConverter x:Key=”BooleanToVisibility” /> … <ListBox Visibility=”{Binding HasItems, RelativeSource={RelativeSource Self}, Converter=BooleanToVisibility}” /> Or as a Trigger so you don’t need the converter: <ListBox> <ListBox.Style> <Style TargetType=”{x:Type ListBox}”> <Setter Property=”Visibility” Value=”Visible” /> <Style.Triggers> <DataTrigger Binding=”{Binding HasItems, RelativeSource={RelativeSource Self}}” Value=”False”> <Setter Property=”Visibility” … Read more

WPF UI element naming conventions

Personally, I find that WPF changes the rules when it comes to this. Often, you can get away with little or no code behind, so having the prefixes to distinguish names makes things more confusing instead of less confusing. In Windows Forms, every control was referenced by name in code. With a large UI, the … Read more

How do you determine if WPF is using Hardware or Software Rendering?

Check RenderCapability.Tier Graphics Rendering Tiers RenderCapability Class [UPDATE] RenderCapability.IsPixelShaderVersionSupported – Gets a value that indicates whether the specified pixel shader version is supported. RenderCapability.IsShaderEffectSoftwareRenderingSupported – Gets a value that indicates whether the system can render bitmap effects in software. RenderCapability.Tier – Gets a value that indicates the rendering tier for the current thread. RenderCapability.TierChanged – … Read more

Default value at design time XAML

Update: Visual Studio 2019 v16.7 You can now do: <TextBlock Text=”{Binding MyText}” d:Text=”Design time value”/> If you would prefer a less verbose version of Ian Bamforth’s answer, you can just do <TextBlock Text=”{Binding MyText, FallbackValue=None}”/>

Provide value on ‘System.Windows.Markup.StaticResourceHolder’ threw an exception

The problem is because of incorrect order of your styles. I mean that if we publish button style with reference on static resource NormalBrush, which is declared under the style, like this: <Style TargetType=”{x:Type Button}”> <Setter Property=”Template”> <Setter.Value> <ControlTemplate TargetType=”{x:Type Button}”> <Border Background=”{StaticResource NormalBrush}” > <ContentPresenter Name=”ButtonCont”></ContentPresenter> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <LinearGradientBrush x:Key=”NormalBrush” StartPoint=”0,0″ … Read more

How do I put a border around an image in WPF?

Simply wrap the Image in a Border control <Border BorderThickness=”1″> <Image Name=”imgPic1″ Width=”100″ Height=”75″ Stretch=”Fill” VerticalAlignment=”Top” /> </Border> You could also provide a style you apply to images that does this if you don’t want to do it around every image Final solution from answer and comments added by Pax: <Border BorderThickness=”1″ BorderBrush=”#FF000000″ VerticalAlignment=”Top”> <Image … Read more

Error Cannot convert lambda expression in subscribe for an IObservable

The namespace System.Reactive.Linq contains the static class Observable which defines all the extension methods for common reactive combinators. It resides in System.Reactive.dll The extension methods for IObservable<T>.Subscribe such as Subscribe(onNext), Subscribe(onNext, onError) are however defined in mscorlib in the static class System.ObservableExtensions. tl;dr: For Rx/Observable extension methods you need to import System.Reactive.Linq = using System.Reactive.Linq; … Read more

Automatically capitalize all input in WPF

You can case all input into TextBox controls with the following property: CharacterCasing=”Upper” To apply to all TextBox controls in the entire application create a style for all TextBox controls: <Style TargetType=”{x:Type TextBox}”> <Setter Property=”CharacterCasing” Value=”Upper”/> </Style>