Why can’t I style a DataGridTextColumn?

You can’t style the DataGridTextColumn because DataGridTextColumn does not derive from FrameworkElement (or FrameworkContentElement). Only FrameworkElement, etc supports styling. When you attempt to create a style in XAML for any type that is not a FrameworkElement or FrameworkContentElement you get that error message. How do you solve this? As with any problem, where there is … Read more

How to display a different value for dropdown list values/selected item in a WPF ComboBox?

Update 2011-11-14 I recently came upon the same requirement again and I wasn’t very happy with the solution I posted below. Here is a nicer way to get the same behavior without re-templating the ComboBoxItem. It uses a DataTemplateSelector First, specify the regular DataTemplate, the dropdown DataTemplate and the ComboBoxItemTemplateSelector in the resources for the … Read more

Creating an image+text button with a control template?

Define a CustomControl like this in .cs public class MyButton : Button { static MyButton() { //set DefaultStyleKeyProperty } public ImageSource ImageSource { get { return (ImageSource)GetValue(ImageSourceProperty); } set { SetValue(ImageSourceProperty, value); } } // Using a DependencyProperty as the backing store for ImageSource. This enables animation, styling, binding, etc… public static readonly DependencyProperty ImageSourceProperty … Read more

WPF MVVM INotifyPropertyChanged Implementation – Model or ViewModel

The thing is that if you were following MVVM, you would have a BookViewModel for your Book model class. So you would have a INotifyPropertyChanged implementation on that view model. Exactly for that purpose MVVM exists (but not only). That being said, the INotifyPropertyChanged has to be implemented on view model classes, not models. UPDATE: … Read more

How to disable highlighting on listbox but keep selection?

<ListBox.ItemContainerStyle> <Style TargetType=”ListBoxItem”> <Setter Property=”IsSelected” Value=”{Binding Content.IsSelected, Mode=TwoWay, RelativeSource={RelativeSource Self}}”/> <Setter Property=”Template”> <Setter.Value> <ControlTemplate TargetType=”ListBoxItem”> <ContentPresenter/> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.ItemContainerStyle>

Set the caret/cursor position to the end of the string value WPF textbox

You can set the caret position using CaretIndex property of a TextBox. Please bear in mind that this is not a DependencyProperty. Nevertheless, you may still set it in XAML like this: <TextBox Text=”123″ CaretIndex=”{x:Static System:Int32.MaxValue}” /> Please remember to set CaretIndex after Text property or else it will not work. Thus it probably won’t … Read more