INotifyPropertyChanged vs. DependencyProperty in ViewModel

Kent wrote an interesting blog about this topic: View Models: POCOs versus DependencyObjects. Short summary: DependencyObjects are not marked as serializable The DependencyObject class overrides and seals the Equals() and GetHashCode() methods A DependencyObject has thread affinity – it can only be accessed on the thread on which it was created I prefer the POCO … Read more

Automatic vertical scroll bar in WPF TextBlock?

Wrap it in a scroll viewer: <ScrollViewer> <TextBlock /> </ScrollViewer> NOTE this answer applies to a TextBlock (a read-only text element) as asked for in the original question. If you want to show scroll bars in a TextBox (an editable text element) then use the ScrollViewer attached properties: <TextBox ScrollViewer.HorizontalScrollBarVisibility=”Disabled” ScrollViewer.VerticalScrollBarVisibility=”Auto” /> Valid values for … Read more

How do I get a TextBox to only accept numeric input in WPF?

Add a preview text input event. Like so: <TextBox PreviewTextInput=”PreviewTextInput” />. Then inside that set the e.Handled if the text isn’t allowed. e.Handled = !IsTextAllowed(e.Text); I use a simple regex in IsTextAllowed method to see if I should allow what they’ve typed. In my case I only want to allow numbers, dots and dashes. private … Read more

Difference between SelectedItem, SelectedValue and SelectedValuePath

Their names can be a bit confusing :). Here’s a summary: The SelectedItem property returns the entire object that your list is bound to. So say you’ve bound a list to a collection of Category objects (with each Category object having Name and ID properties). eg. ObservableCollection<Category>. The SelectedItem property will return you the currently … Read more

How to get StackPanel’s children to fill maximum space downward?

It sounds like you want a StackPanel where the final element uses up all the remaining space. But why not use a DockPanel? Decorate the other elements in the DockPanel with DockPanel.Dock=”Top”, and then your help control can fill the remaining space. XAML: <DockPanel Width=”200″ Height=”200″ Background=”PowderBlue”> <TextBlock DockPanel.Dock=”Top”>Something</TextBlock> <TextBlock DockPanel.Dock=”Top”>Something else</TextBlock> <DockPanel HorizontalAlignment=”Stretch” VerticalAlignment=”Stretch” … Read more

The calling thread cannot access this object because a different thread owns it

This is a common problem with people getting started. Whenever you update your UI elements from a thread other than the main thread, you need to use: this.Dispatcher.Invoke(() => { …// your code here. }); You can also use control.Dispatcher.CheckAccess() to check whether the current thread owns the control. If it does own it, your … Read more

How to bind inverse boolean properties in WPF?

You can use a ValueConverter that inverts a bool property for you. XAML: IsEnabled=”{Binding Path=IsReadOnly, Converter={StaticResource InverseBooleanConverter}}” Converter: [ValueConversion(typeof(bool), typeof(bool))] public class InverseBooleanConverter: IValueConverter { #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (targetType != typeof(bool)) throw new InvalidOperationException(“The target must be a boolean”); return !(bool)value; } public … Read more

Storing WPF Image Resources

If you will use the image in multiple places, then it’s worth loading the image data only once into memory and then sharing it between all Image elements. To do this, create a BitmapSource as a resource somewhere: <BitmapImage x:Key=”MyImageSource” UriSource=”../Media/Image.png” /> Then, in your code, use something like: <Image Source=”{StaticResource MyImageSource}” /> In my … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)