WPF MVVM 数据绑定问题
Posted
技术标签:
【中文标题】WPF MVVM 数据绑定问题【英文标题】:WPF MVVM DataBinding issue 【发布时间】:2013-02-15 22:52:39 【问题描述】:有人可以解释为什么在使用以下代码实现的 WPF 扩展控件“BusyIndicator”中未使用在 CodeBehind 中分配的 ViewModel:
ViewModel 实现:
public partial class App : Application
private void OnStartup(object sender, StartupEventArgs seArgs)
MainWindowView mwv = new MainWindowView();
mwv.DataContext = new DataSetViewModel(new DataSetModel());
mwv.Show();
XAML:
<kit:BusyIndicator IsBusy="True" DisplayAfter="0" >
<kit:BusyIndicator.BusyContentTemplate>
<DataTemplate>
<StackPanel Margin="4">
<TextBlock Text="Binding Path=WaitProgressUploadTitle" FontWeight="Bold" HorizontalAlignment="Center"/>
<StackPanel Margin="4">
<TextBlock Text="Binding Path=WaitProgressUploadContent" />
<ProgressBar Value="40" Height="15"/>
</StackPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="Pause" HorizontalAlignment="Right" Margin="0 0 2 0"/>
<Button Grid.Column="1" Content="Cancel" HorizontalAlignment="Left" Margin="2 0 0 0"/>
</Grid>
</StackPanel>
</DataTemplate>
</kit:BusyIndicator.BusyContentTemplate>
<kit:BusyIndicator.OverlayStyle>
<Style TargetType="Rectangle">
<Setter Property="Fill" Value="#ffffeeee"/>
</Style>
</kit:BusyIndicator.OverlayStyle>
<kit:BusyIndicator.ProgressBarStyle>
<Style TargetType="ProgressBar">
<Setter Property="Visibility" Value="Collapsed"/>
</Style>
</kit:BusyIndicator.ProgressBarStyle>
<StackPanel>
<kitdg:DataGridControl Name="DataPreview" ItemsSource="Binding Path=dvLbSelection" Visibility="Binding Path=FileOpenBad,Converter=StaticResource errorVisibility" ReadOnly="True" />
<TextBlock Text="Binding Path=FileOpenText" Margin="0,100,0,0" HorizontalAlignment="Center" MaxWidth="675" TextAlignment="Center" FontSize="20" Visibility="Binding Path=FileOpenGood,Converter=StaticResource errorVisibility" />
</StackPanel>
</kit:BusyIndicator>
在 VS2010 的输出窗口中显示以下内容:
System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“字符串”(HashCode=-1880398257)上找不到“WaitProgressUploadTitle”属性。绑定表达式:路径=WaitProgressUploadTitle; DataItem='String' (HashCode=-1880398257);目标元素是'TextBlock'(名称='');目标属性是“文本”(类型“字符串”) System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“字符串”(HashCode=-1880398257)上找不到“WaitProgressUploadContent”属性。 BindingExpression:Path=WaitProgressUploadContent; DataItem='String' (HashCode=-1880398257);目标元素是'TextBlock'(名称='');目标属性是“文本”(类型“字符串”) System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“DataSetViewModel”(HashCode=3476046)上找不到“connectionStringValidity”属性。绑定表达式:路径=连接字符串有效性; DataItem='DataSetViewModel' (HashCode=3476046);目标元素是“图像”(名称=“errorInvalidSQLConnection”);目标属性是“工具提示”(类型“对象”)
【问题讨论】:
【参考方案1】:您没有在BusyIndicator
控件上设置DataContext
。您将其设置在 MainWindowView
对象上。您可以做的是在内部命名您的MainWindowView
,并像这样设置您的BusyIndicator
控件的DataContext
(显然用您在内部给您窗口的任何名称替换NameOfMainWindowView
):
<kit:BusyIndicator DataContext="Binding DataContext, ElementName=NameOfMainWindowView"
IsBusy="True" DisplayAfter="0" >
【讨论】:
抱歉,我错过了Binding
。以上是关于WPF MVVM 数据绑定问题的主要内容,如果未能解决你的问题,请参考以下文章