Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: Binding to single complex item
    primarykey
    data
    text
    <p>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.</p> <p>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.)</p> <p>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. <code>null</code>), it uses the fallback values.</p> <p>What I actually wanted is to display the ViewModel's contents.</p> <hr> <p>View model:</p> <pre><code>public class ImportInformation : INotifyPropertyChanged { /* ... */ } </code></pre> <p>User control:</p> <pre><code>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(); } } </code></pre> <p>XAML:</p> <pre><code>&lt;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"&gt; &lt;UserControl.Resources&gt; &lt;conv:NaNConverter x:Key="nanConv"/&gt; &lt;/UserControl.Resources&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;TextBlock Padding="0,4,0,4" TextWrapping="Wrap"&gt; Your import is currently being processed. Depending on the data source, this can take a long, &lt;Italic&gt;long&lt;/Italic&gt; time. &lt;/TextBlock&gt; &lt;GroupBox VerticalAlignment="Top" HorizontalAlignment="Left" MinWidth="150" DataContext="{Binding ElementName=root, Path=Value}"&gt; &lt;GroupBox.Header&gt; &lt;TextBlock FontWeight="Bold" Text="{Binding Path=TaskName, Mode=OneWay, FallbackValue=Initializing...}"/&gt; &lt;/GroupBox.Header&gt; &lt;StackPanel Orientation="Vertical" Margin="4"&gt; &lt;TextBlock TextWrapping="Wrap" Text="{Binding Path=CurrentTask, Mode=OneWay, FallbackValue=Initializing...}" Margin="0,0,0,2"/&gt; &lt;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}"/&gt; &lt;TextBlock FontSize="11" HorizontalAlignment="Right" Text="{Binding Path=Progress, Mode=OneWay, FallbackValue=0, StringFormat=\{0:P\}}"/&gt; &lt;/StackPanel&gt; &lt;/GroupBox&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload