WhyContentControl在WPF中隐藏了Label的一些功能?

时间:2014-08-02 14:04:06

标签: c# wpf xaml mvvm

我是WPF的初学者。下面是我在WPF中的View代码。如果我运行确切的代码我的标签' Error_Label'在运行时隐藏或不起作用。还有GridView停止反映我的数据。但是,如果我只删除3个标签ContentControl,ContentControl.ContentTemplate,带触发器的DataTemplate它可以正常工作。我可以知道这背后的原因吗?

不工作版

<Window x:Class="Seris.VehicalForm"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="500" Width="600">
<ContentControl>
    <ContentControl.ContentTemplate>
        <DataTemplate>
            <WrapPanel Orientation="Vertical" Margin="10 " >
                <Label Content="Vehical No" HorizontalAlignment="Left"/>
                <TextBox Name="VehicalNo_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding VehicalNo}"  HorizontalAlignment="Left" />
                <Label Content="Model" HorizontalAlignment="Left"/>
                <TextBox Name="Model_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding Model}" HorizontalAlignment="Left" />
                <Label Content="Manufacturing Date" HorizontalAlignment="Left"/>
                <DatePicker Name="ManufacturingDate_DateTime" SelectedDate="{Binding ManufacturingDate, Mode=TwoWay}"/>
                <Label Content="IU No" HorizontalAlignment="Left"/>
                <TextBox Height="23" Width="80" Name="IUNO_Text" TextWrapping="Wrap" Text="{Binding IUNo}" HorizontalAlignment="Left"/>
                <Label Content="Personnel" HorizontalAlignment="Left"/>
                <ComboBox Name="Personnel_Combo" Text="{Binding Personnel}" HorizontalAlignment="Left" Width="116"/>
                <Separator Height="20" RenderTransformOrigin="0.5,0.5" Width="16"/>
                <Button Name="Save_Button" Command="{Binding SaveButton_Command}" Content="Save" Width="66"/>
                <Label x:Name="Error_Label" Content="{Binding ErrorMessage, UpdateSourceTrigger=PropertyChanged}" Foreground="Red" HorizontalAlignment="Left" Height="41" Width="137"/>
                <ListView  Name ="Grid" Height="294" Width="371" ItemsSource="{Binding ListItems, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" >
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Vehical No" DisplayMemberBinding="{Binding VehicalNo}" />
                            <GridViewColumn Header="Model" DisplayMemberBinding="{Binding Model}" />
                            <GridViewColumn Header="ManufacturingDate" DisplayMemberBinding="{Binding ManufacturingDate}" />
                            <GridViewColumn Header="IUNo" DisplayMemberBinding="{Binding IUNo}" />
                            <GridViewColumn Header="Personnel" DisplayMemberBinding="{Binding Personnel}" />
                        </GridView>
                   </ListView.View>
                </ListView>
                <Label Name="Notification"/>
            </WrapPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding IsKeyboardFocused, ElementName=Grid}" Value="true">
                    <Setter Property="Content" TargetName="Notification">
                        <Setter.Value>
                            <MultiBinding StringFormat="{}Name is '{0}'}">
                                           <Binding ElementName="Notification" Path="Text" />
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ContentControl.ContentTemplate>
</ContentControl>
</Window>

工作版

<Window x:Class="Seris.VehicalForm"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="500" Width="600">

            <WrapPanel Orientation="Vertical" Margin="10 " >
                <Label Content="Vehical No" HorizontalAlignment="Left"/>
                <TextBox Name="VehicalNo_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding VehicalNo}"  HorizontalAlignment="Left" />
                <Label Content="Model" HorizontalAlignment="Left"/>
                <TextBox Name="Model_Text" Height="23" Width="80" TextWrapping="Wrap" Text="{Binding Model}" HorizontalAlignment="Left" />
                <Label Content="Manufacturing Date" HorizontalAlignment="Left"/>
                <DatePicker Name="ManufacturingDate_DateTime" SelectedDate="{Binding ManufacturingDate, Mode=TwoWay}"/>
                <Label Content="IU No" HorizontalAlignment="Left"/>
                <TextBox Height="23" Width="80" Name="IUNO_Text" TextWrapping="Wrap" Text="{Binding IUNo}" HorizontalAlignment="Left"/>
                <Label Content="Personnel" HorizontalAlignment="Left"/>
                <ComboBox Name="Personnel_Combo" Text="{Binding Personnel}" HorizontalAlignment="Left" Width="116"/>
                <Separator Height="20" RenderTransformOrigin="0.5,0.5" Width="16"/>
                <Button Name="Save_Button" Command="{Binding SaveButton_Command}" Content="Save" Width="66"/>
                <Label x:Name="Error_Label" Content="{Binding ErrorMessage, UpdateSourceTrigger=PropertyChanged}" Foreground="Red" HorizontalAlignment="Left" Height="41" Width="137"/>
                <ListView  Name ="Grid" Height="294" Width="371" ItemsSource="{Binding ListItems, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" >
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Vehical No" DisplayMemberBinding="{Binding VehicalNo}" />
                            <GridViewColumn Header="Model" DisplayMemberBinding="{Binding Model}" />
                            <GridViewColumn Header="ManufacturingDate" DisplayMemberBinding="{Binding ManufacturingDate}" />
                            <GridViewColumn Header="IUNo" DisplayMemberBinding="{Binding IUNo}" />
                            <GridViewColumn Header="Personnel" DisplayMemberBinding="{Binding Personnel}" />
                        </GridView>
                   </ListView.View>
                </ListView>
                <Label Name="Notification"/>
            </WrapPanel>

</Window>

1 个答案:

答案 0 :(得分:0)

Datatrigger

似乎错误
 <DataTemplate.Triggers>
    <DataTrigger Binding="{Binding IsKeyboardFocused, ElementName=Grid}" Value="true">
        <Setter Property="Content" TargetName="Notification" Value="{Binding Path=Text,StringFormat={},Name is'{0}'}"/>
    </DataTrigger>
</DataTemplate.Triggers>