WPF MVVM DataBinding问题

时间:2013-02-15 22:52:39

标签: wpf mvvm binding

有人可以解释为什么在使用以下代码实现的WPF扩展控件'BusyIndi​​cator'中没有使用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路径错误:'object'''String'(HashCode = -1880398257)'找不到'WaitProgressUploadTitle'属性。 BindingExpression:路径= WaitProgressUploadTitle; DataItem ='String'(HashCode = -1880398257); target元素是'TextBlock'(Name =''); target属性是'Text'(类型'String')   System.Windows.Data错误:40:BindingExpression路径错误:'object'''String'(HashCode = -1880398257)'上找不到'WaitProgressUploadContent'属性。 BindingExpression:路径= WaitProgressUploadContent; DataItem ='String'(HashCode = -1880398257); target元素是'TextBlock'(Name =''); target属性是'Text'(类型'String')   System.Windows.Data错误:40:BindingExpression路径错误:'object'''DataSetViewModel'(HashCode = 3476046)'上找不到'connectionStringValidity'属性。 BindingExpression:路径= connectionStringValidity; DataItem ='DataSetViewModel'(HashCode = 3476046); target元素是'Image'(Name ='errorInvalidSQLConnection'); target属性是'ToolTip'(类型'Object')

1 个答案:

答案 0 :(得分:0)

您未在DataContext控件上设置BusyIndicator。您在MainWindowView对象上进行设置。你可以做的就是在内部命名你的MainWindowView,然后设置你DataContext控件的BusyIndicator(显然用内部提供给你窗口的名字替换NameOfMainWindowView) :

<kit:BusyIndicator DataContext="{Binding DataContext, ElementName=NameOfMainWindowView}"
IsBusy="True" DisplayAfter="0" >