如何在WPF MVVM中修复Binding TextBlock?

时间:2019-04-07 14:54:20

标签: wpf xaml user-interface mvvm binding

您会在底部看到我的XAML代码;当我在运行时调试它时,在窗口的StackPanel的两个TextBlocks中看不到任何东西,而绑定的TextBlocks在运行时却有一些数据! 为什么在那些TextBlocks中什么也看不到?

<Grid Name="SchoolGridView"
      AutomationProperties.AutomationId="SchoolGridView"
      AutomationProperties.Name="School Group" >
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <ListView Grid.Row="0" Height="Auto"  x:Name="List" 
     HorizontalContentAlignment="Center" ItemsSource="{Binding 
     PeopleList}" SelectionMode="Single">

           ...
    </ListView>
    <StackPanel Background="LightGray" Height="60" 
       Orientation="Horizontal"  VerticalAlignment="Bottom" Grid.Row="1" 
       Visibility="{Binding 
       IsDetailVisible,UpdateSourceTrigger=PropertyChanged}">
            <StackPanel HorizontalAlignment="Left" Width="320">
                <TextBlock  Height="20" Text="asaasdasd"></TextBlock>
                <TextBlock  Height="20" HorizontalAlignment="Left" 
         Background="Yellow" Text="{Binding DataContext.FullName, 
         UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock  Height="20" HorizontalAlignment="Left" 
           Background="Yellow" Text="{Binding 
           DataContext.RowSelectedItem, 
             UpdateSourceTrigger=PropertyChanged}"/>
            </StackPanel>
            <WrapPanel HorizontalAlignment="Right">
                <Button  Content="DELETE" HorizontalAlignment="Right" 
             VerticalAlignment="Top"
                         Width="auto"  Height="50" Margin="3" Command=" 
             {Binding DataContext.DeleteCommand, ElementName=List}" 
             CommandParameter="{Binding SelectedItem, 
             ElementName=List}"/>
            </WrapPanel>
       </StackPanel>
     </Grid>
   </Window>

1 个答案:

答案 0 :(得分:0)

You must fill DataContext

<Window x:Class="SampleApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SampleApplication"
Title="MainWindow" Height="350" Width="525">

Or set it in Code

 DataContext = new ViewModel();