[英]WPF: Binding to single complex item
在我的應用程序中,我使用UserControl實例來顯示實現INotifyPropertyChanged的復雜類型的內容,該類型是一個ViewModel來顯示后台進程的當前狀態。 此ViewModel作為DependencyProperty公開給UserControl。
很好地設置了DependencyProperty,並且ViewModel高興地發送了事件。 但是,WPF的數據綁定似乎讓我有些頭疼。 (此外,我不會把自己當做WPF的專家;這個問題及其解決方案可能很簡單,但這使我難以理解。)
基本上,我看到的只是UI上的“ Initialzing”,這是DependencyProperty的默認值,以及用於綁定的后備值。 如果更改DependencyProperty的默認值,則UI會顯示這些值。 如果刪除DependencyProperty的默認值(即null
),它將使用后備值。
我真正想要的是顯示ViewModel的內容。
查看模型:
public class ImportInformation : INotifyPropertyChanged
{
/* ... */
}
用戶控件:
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>
public static readonly DependencyProperty ValueProperty = DependencyProperty
.Register("Value", ...);
...
<TextBlock
...
Text="{Binding ElementName=root, ...}"/>
...
我的錯。 我注冊了DependencyProperty錯誤:
public static readonly DependencyProperty ValueProperty = DependencyProperty
.Register("ValueProperty", typeof(ImportInformation), typeof(ImportTab));
...應該讀過...
public static readonly DependencyProperty ValueProperty = DependencyProperty
.Register("Value", typeof(ImportInformation), typeof(ImportTab));
那當然可以解決。 謝謝你:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.