Hi there,
In my application, I use a UserControl instance to display the contents of a complex type that implements INotifyPropertyChanged that is a ViewModel to display the current state of background processes. This ViewModel is exposed to the UserControl as a DependencyProperty.
The DependencyProperty is being set nicely, and events are happily sent out by the ViewModel. However, it seems that WPF's data binding is giving me a slight headache. (Plus, I wouldn't count myself as an expert on WPF; the problem and its solution may be simple, but it's eluding me.)
Basically, all I see is "Initialzing" on my UI, which is the DependencyProperty's default value, and the fallback value used for binding. If I alter the DependencyProperty's default value, the UI shows these values instead. If I remove the DependencyProperty's default value (ie. null
), it uses the fallback values.
What I actually wanted is to display the ViewModel's contents.
View model:
public class ImportInformation : INotifyPropertyChanged
{
/* ... */
}
User control:
public partial class ImportTab : UserControl
{
public static readonly DependencyProperty ValueProperty = DependencyProperty
.Register("ValueProperty", typeof(ImportInformation), typeof(ImportTab), new PropertyMetadata(new ImportInformation { TaskName = "Initializing...", CurrentTask = "Initializing...", Progress = Double.NaN }));
public ImportInformation Value
{
get { return (ImportInformation)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public ImportTab()
{
InitializeComponent();
}
}
XAML:
<UserControl x:Class="LingImp.Tabs.ImportTab"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:conv="clr-namespace:LingImp.Converters"
mc:Ignorable="d" Name="root"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<conv:NaNConverter x:Key="nanConv"/>
</UserControl.Resources>
<StackPanel Orientation="Vertical">
<TextBlock Padding="0,4,0,4" TextWrapping="Wrap">
Your import is currently being processed. Depending on the data source, this can take a long, <Italic>long</Italic> time.
</TextBlock>
<GroupBox VerticalAlignment="Top" HorizontalAlignment="Left" MinWidth="150" DataContext="{Binding ElementName=root, Path=Value}">
<GroupBox.Header>
<TextBlock FontWeight="Bold" Text="{Binding Path=TaskName, Mode=OneWay, FallbackValue=Initializing...}"/>
</GroupBox.Header>
<StackPanel Orientation="Vertical" Margin="4">
<TextBlock TextWrapping="Wrap" Text="{Binding Path=CurrentTask, Mode=OneWay, FallbackValue=Initializing...}"
Margin="0,0,0,2"/>
<ProgressBar MinHeight="9" HorizontalAlignment="Stretch" Minimum="0" Maximum="1"
IsIndeterminate="{Binding Path=Progress, Mode=OneWay, FallbackValue=true, Converter={StaticResource nanConv}}"
Value="{Binding ElementName=root, Path=Value.Progress, Mode=OneWay, FallbackValue=0}"/>
<TextBlock FontSize="11" HorizontalAlignment="Right"
Text="{Binding Path=Progress, Mode=OneWay, FallbackValue=0, StringFormat=\{0:P\}}"/>
</StackPanel>
</GroupBox>
</StackPanel>
</UserControl>