设置网格的可见性

时间:2013-09-24 12:21:36

标签: c# wpf mvvm grid

我正在使用mvvm。我有一个主页,我有2个网格,一个内部和另一个外部..在父网格我有一个数据网格。当我点击数据网格的行我需要使子网格包含可见绑定到点击的行值的控件。帮助我,我没有得到任何想法。请帮助我..请帮助..

这是我的xaml ..

<Window x:Class="EmployeeRegistration.Home"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:View="clr-namespace:EmployeeRegistration" WindowState="Maximized"

        Title="Home" Height="853" Width="1200"
        >
    <Window.Background>
        <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
            <GradientStop Color="Black" Offset="0" />
            <GradientStop Color="#FF84BFC4" Offset="1" />
        </LinearGradientBrush>
    </Window.Background>
    <Window.DataContext>
        <View:EmployeeViewModel></View:EmployeeViewModel>
    </Window.DataContext>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <cmd:EventToCommand Command="{Binding GetEmployeeCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Window.Resources>
        <Style TargetType="Control" x:Key="myErrorTemplate">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <Border BorderBrush="Red" BorderThickness="1" CornerRadius="2.75" Grid.Column="0">
                            <AdornedElementPlaceholder Name="MyControl" Grid.Column="0"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
                Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="TextBox" BasedOn="{StaticResource myErrorTemplate}" />
    </Window.Resources>
    <Grid Height="677" Width="1139"  Background="#FFC4DDD1">
        <Grid.RowDefinitions>
            <RowDefinition Height="2"></RowDefinition>
            <RowDefinition Height="48*"></RowDefinition>
            <RowDefinition Height="65" />
            <RowDefinition Height="457*" />
            <!--<RowDefinition Height="35" />
            <RowDefinition Height="44"></RowDefinition>
            <RowDefinition Height="45" />
            <RowDefinition Height="49" />
            <RowDefinition Height="56" />
            <RowDefinition Height="43" />
            <RowDefinition Height="40" />
            <RowDefinition Height="37"></RowDefinition>
            <RowDefinition Height="39" />
            <RowDefinition Height="83*" />-->
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="538" />
            <ColumnDefinition Width="538"/>

        </Grid.ColumnDefinitions>

        <Label Name="lblEmployeeDetails" Content="EMPLOYEE DETAILS" FontFamily="Calibri" FontSize="26" HorizontalAlignment="Center" Margin="117,10,125,0" Width="296" Grid.Row="1"></Label>



        <TextBlock Margin="0,-2,-6,9" HorizontalAlignment="Right" Width="141" Grid.Column="4" Grid.RowSpan="2">
            <Button Name="btnNewEmployee"  Content="New Employee Registration" FontFamily="Calibri" Background="#FF2693A7" Foreground="White" />
        </TextBlock>
        <Label Name="lblEnterName" Grid.Row="2" Content="Enter Employee Name:" FontFamily="Calibri" FontSize="16" Margin="0,16,12,23"></Label>
        <TextBox Name="txtSearch" Text="{Binding Path=FirstName,Mode=OneWayToSource,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Grid.Row="2" Margin="166,18,0,21" HorizontalAlignment="Left" Width="186"></TextBox>
        <Button Name="btnSearch" Command="{Binding SearchEmployeeCommand}" Grid.Row="2" Content="Search" Margin="0,18,58,21" HorizontalAlignment="Right" Width="103"  Foreground="White" Background="Black"></Button>

        <DataGrid Grid.Row="3" x:Name="grdEmployee" SelectedItem="{Binding SelectedEmployee, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding EmployeeDatatable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" AutoGenerateColumns="False"  IsReadOnly="True" FontFamily="Calibri" HorizontalAlignment="Center" Margin="5,26,39,0" Width="494" CanUserAddRows="False"  Background="#FF20E0BB" AlternatingRowBackground="#FF7CB4AB" Height="420" VerticalAlignment="Top">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Emp_id}" Header="Employee ID"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding FirstName}" Header="FirstName"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding LastName}" Header="LastName"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Age}" Header="Age"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding ZipCode}" Header="ZipCode"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding PhoneNumber}" Header="PhoneNumber"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding MobileNumber}" Header="MobileNumber"></DataGridTextColumn>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Edit" Command="{Binding DataContext.SelectEmployeeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding SelectedEmployee}"  ></Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <Grid Grid.Row="1" Margin="532,10,-35,12" Grid.RowSpan="3" Grid.ColumnSpan="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="142" />
                <ColumnDefinition Width="139" />
                <ColumnDefinition Width="138" />

                <ColumnDefinition Width="154" />
                <ColumnDefinition Width="0*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="67" />
            <RowDefinition Height="37"></RowDefinition>
            <RowDefinition Height="36" />
                <RowDefinition Height="40" />
                <RowDefinition Height="40" />
            <RowDefinition Height="44" />
            <RowDefinition Height="52" />
            <RowDefinition Height="54" />
            <RowDefinition Height="50"></RowDefinition>
                <RowDefinition Height="50*" />
                <RowDefinition Height="47*" />
                <RowDefinition Height="42*" />
                <RowDefinition Height="106*" />
            </Grid.RowDefinitions>

            <Label Name="lblHeading" HorizontalAlignment="Center" Content="UPDATE EMPLOYEE DETAILS" FontFamily="Calibri" FontSize="22"  VerticalAlignment="Center"  Height="71" Width="263" Grid.ColumnSpan="2" Margin="2,21,12,12" Grid.RowSpan="2" Grid.Column="1">
            </Label>
            <Label Content="*" Grid.Row="2" Height="25" HorizontalAlignment="Right" Margin="0,0,15,12" Name="lblReqField" VerticalAlignment="Bottom" Width="49" Foreground="Red" />
            <Label Name="lblPersonalInfo" Content="PERSONAL INFO" FontFamily="Calibri" FontSize="20" Foreground="#FF1C48D8" Grid.Row="1" Height="37" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="270" Grid.ColumnSpan="2" />
            <Label Grid.Row="1" Height="37" HorizontalAlignment="Left" Name="lblFirstName" Content="FirstName"  FontFamily="Calibri" FontSize="16" VerticalAlignment="Bottom" Width="80" Grid.RowSpan="2" />
            <TextBox Grid.Column="1" Grid.Row="2" Height="25" HorizontalAlignment="Left" Name="txtFirstName" Text="{Binding Path=FirstName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"  BorderBrush="#FF179EB7"  VerticalAlignment="Top" Width="130" Margin="0,3,0,0" />

            <Label Content="*" Grid.Row="3" Height="25" HorizontalAlignment="Right" Margin="0,0,15,12" Name="lblReqField1" VerticalAlignment="Bottom" Width="49" Foreground="Red" />
            <Label  Height="35" HorizontalAlignment="Left" Name="lblLastName" Content="LastName"  FontFamily="Calibri" FontSize="16" VerticalAlignment="Top" Width="142" Grid.Row="3" />
            <TextBox BorderBrush="#FF179EB7" Height="25" HorizontalAlignment="Left" Name="txtLastName" Text="{Binding Path=LastName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" VerticalAlignment="Top" Width="130" Grid.Column="1" Grid.Row="3" Margin="1,4,0,0" />

            <Label Content="*" Grid.Row="4" Height="25" HorizontalAlignment="Right" Margin="0,0,86,12" Name="lblReqField2" VerticalAlignment="Bottom" Width="19" Foreground="Red" />
            <Label  Height="35" HorizontalAlignment="Left" Name="lblDob" Content="DOB"  FontFamily="Calibri" FontSize="16"  VerticalAlignment="Top" Width="56" Grid.Column="0" Grid.Row="4" Grid.RowSpan="2" Grid.ColumnSpan="2" />
            <DatePicker Grid.Column="1" Grid.Row="4" Name="dtpDob" Text="{Binding Path=Dob,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Height="25" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="129"  />


            <Label  Height="28" HorizontalAlignment="Left" Name="lblAge" Content="Age"  FontFamily="Calibri" FontSize="16"  VerticalAlignment="Top" Width="142" Grid.Row="5" Margin="0,8,0,0" />
            <TextBox Height="25" HorizontalAlignment="Left" Name="txtAge" BorderBrush="#FF179EB7" IsReadOnly="True"  Text="{Binding Path=Age,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" VerticalAlignment="Top" Width="138" Grid.Row="5" Margin="1,11,0,0" Grid.Column="1" />


            <Label  Height="45" HorizontalAlignment="Left" Name="lblAddressInfo" Content="ADDRESS INFO" FontFamily="Calibri" FontSize="20"  VerticalAlignment="Top" Width="142" Grid.Row="6" Margin="0,7,0,0" />


            <Label Content="*" Grid.Row="7" Height="25" HorizontalAlignment="Right" Margin="0,0,39,27" Name="lblReqField3" VerticalAlignment="Bottom" Width="49" Foreground="Red" />
            <Label  Height="35" HorizontalAlignment="Left" Name="lblStreet1" Content="Street1"  FontFamily="Calibri" FontSize="16" VerticalAlignment="Top" Width="56" Grid.Column="0" Grid.Row="7" />
            <TextBox  Name="txtStreet1" BorderBrush="#FF179EB7" Text="{Binding Path=Street1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Height="25" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="129" Grid.Column="1" Grid.Row="7"  />

            <Label Content="*" Grid.Row="8" Height="25" HorizontalAlignment="Right" Margin="0,0,39,25" Name="lblReqField4" VerticalAlignment="Bottom" Width="49" Foreground="Red" />
            <Label  Height="35" HorizontalAlignment="Left" Name="lblStreet2" Content="Street2"  FontFamily="Calibri" FontSize="16" VerticalAlignment="Top" Width="56" Grid.Column="0" Grid.Row="8"/>
            <TextBox  Name="txtStreet2" BorderBrush="#FF179EB7" Text="{Binding Path=Street2, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Height="25" HorizontalAlignment="Left" VerticalAlignment="Top" Width="130" Grid.Column="1" Grid.Row="8" />

            <Label  Height="35" HorizontalAlignment="Left" Name="lblCity" Content="City"  FontFamily="Calibri" FontSize="16"   VerticalAlignment="Top" Width="49" Grid.Row="9"  />
            <ComboBox Text="{Binding Path=City,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Name="cmbCity" Width="129" Height="25" BorderBrush="#FF179EB7" Grid.Column="1" Grid.Row="9" HorizontalAlignment="Left"   VerticalAlignment="Top" >
                <ComboBoxItem Content="Kasargod"></ComboBoxItem>
                <ComboBoxItem Content="Kannur"></ComboBoxItem>
                <ComboBoxItem Content="Thrissur"></ComboBoxItem>
                <ComboBoxItem Content="Ernakulam"></ComboBoxItem>
                <ComboBoxItem Content="Palakkad"></ComboBoxItem>
                <ComboBoxItem Content="Alappuzha"></ComboBoxItem>
                <ComboBoxItem Content="Chennai"></ComboBoxItem>
                <ComboBoxItem Content="WhiteField"></ComboBoxItem>
                <ComboBoxItem Content="Bangalore"></ComboBoxItem>
            </ComboBox>

            <Label  Height="104" HorizontalAlignment="Left" Name="lblState" Content="State"  FontFamily="Calibri" FontSize="16" Margin="0,49,0,0"  VerticalAlignment="Top" Width="142" Grid.Row="9" Grid.RowSpan="4" />
            <ComboBox Height="25" HorizontalAlignment="Left" Margin="2,0,0,0"  VerticalAlignment="Top" Width="129" Grid.Column="1" Grid.Row="10" Text="{Binding Path=State, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Name="cmbState" BorderBrush="#FF179EB7">
                <ComboBoxItem Content="Kerala"></ComboBoxItem>
                <ComboBoxItem Content="Karnataka"></ComboBoxItem>
                <ComboBoxItem Content="TamilNadu"></ComboBoxItem>
                <ComboBoxItem Content="AndhraPradesh"></ComboBoxItem>
                <ComboBoxItem Content="Rajastan"></ComboBoxItem>
            </ComboBox>


            <Label  Height="42" HorizontalAlignment="Left"  Name="lblZipcode" Content="ZipCode"  FontFamily="Calibri" FontSize="16"  VerticalAlignment="Top" Width="142" Grid.Row="11"  />
            <TextBox Name="txtZipcode" BorderBrush="#FF179EB7" Text="{Binding Path=ZipCode,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Height="25"   VerticalAlignment="Bottom" Width="130" Grid.Row="11" Grid.Column="1" HorizontalAlignment="Left" Margin="1,0,0,17" />

            <Label  Height="35" HorizontalAlignment="Left"  Name="lblContactInfo" Content="CONTACT INFO" FontFamily="Calibri" FontSize="20"  VerticalAlignment="Top" Width="138" Grid.Column="2" Grid.Row="6" />


            <Label  Height="35" HorizontalAlignment="Left" Name="lblPhoneNumber" Content="PhoneNumber"  FontFamily="Calibri" FontSize="16" VerticalAlignment="Top" Width="110" Grid.Column="2" Grid.Row="7" />
            <TextBox Height="25" Name="txtPhoneNo"  BorderBrush="#FF179EB7" Text="{Binding Path=PhoneNumber,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" HorizontalAlignment="Right" VerticalAlignment="Top" Width="125" Grid.Column="3" Grid.Row="7" Margin="0,4,0,0" />



            <Label  Height="35" HorizontalAlignment="Left" Name="lblMobileNumber" Content="MobileNumber"  FontFamily="Calibri" FontSize="16"  VerticalAlignment="Top" Width="109" Grid.Column="2" Grid.Row="8"  />
            <TextBox Name="txtMobileNo" BorderBrush="#FF179EB7" Text="{Binding Path=MobileNumber,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Height="25" HorizontalAlignment="Left"   VerticalAlignment="Top" Width="125" Grid.Column="3" Grid.Row="8" Margin="29,0,0,0" />

            <Label  Height="35" HorizontalAlignment="Left" Name="lblEmail" Content="Email"  FontFamily="Calibri" FontSize="16"  VerticalAlignment="Top" Width="56" Grid.Column="2" Grid.Row="9" />
            <TextBox Name="txtEmail" BorderBrush="#FF179EB7" Text="{Binding Path=Email,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Height="25" HorizontalAlignment="Left"   VerticalAlignment="Top" Width="125" Grid.Column="3" Grid.Row="9" Margin="29,4,0,0" />

            <Label  Height="35" HorizontalAlignment="Left" Name="lblWeb" Content="Web"  FontFamily="Calibri" FontSize="16" VerticalAlignment="Top" Width="56" Grid.Column="2" Grid.Row="10" />
            <TextBox Name="txtWeb" BorderBrush="#FF179EB7" Text="{Binding Path=Web,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Height="25" HorizontalAlignment="Left"   VerticalAlignment="Top" Width="125" Grid.Column="3" Grid.Row="10" Margin="29,4,0,0" />

            <Button Content="Update" Command="{Binding UpdateDetailsCommand}" IsEnabled="{Binding Path=AllPropertiesValid}" Grid.Column="2" Grid.Row="11" Height="25" HorizontalAlignment="Left" Margin="40,9,0,0" Name="btnUpdate" VerticalAlignment="Top" Width="70" Background="Black" Foreground="White">
                <Button.OpacityMask>
                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                        <GradientStop Color="Black" Offset="0" />
                        <GradientStop Color="#FF3870DB" Offset="1" />
                    </LinearGradientBrush>
                </Button.OpacityMask>
            </Button>
            <Button Content="Cancel" Height="25" HorizontalAlignment="Left" Margin="0,9,0,0" Name="btnCancel" VerticalAlignment="Top" Width="70" Grid.Column="3" Grid.Row="11" Background="Black" Foreground="White">
                <Button.OpacityMask>
                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                        <GradientStop Color="Black" Offset="0" />
                        <GradientStop Color="#FF29289D" Offset="1" />
                    </LinearGradientBrush>
                </Button.OpacityMask>
            </Button>
            <Label Content="+91" Grid.Row="7" Height="44" HorizontalAlignment="Left" Margin="132,4,0,0" Name="label2" VerticalAlignment="Top" Width="33" Grid.Column="2" Grid.ColumnSpan="2" />
            <Label Content="*" Foreground="Red" Height="25" HorizontalAlignment="Right" Margin="0,0,62,23" Name="label1" VerticalAlignment="Bottom" Width="49" Grid.Row="9" />
            <Label Content="*" Foreground="Red" Height="25" HorizontalAlignment="Right" Margin="0,0,39,23" Name="label3" VerticalAlignment="Bottom" Width="49" Grid.Row="9" Grid.RowSpan="2" />
            <Label Content="*" Foreground="Red" Height="25" HorizontalAlignment="Right" Margin="0,0,39,17" Name="label4" VerticalAlignment="Bottom" Width="49" Grid.Row="11" />
            <Label Content="*" Foreground="Red" Height="25" HorizontalAlignment="Right" Margin="0,0,12,27" Name="label5" VerticalAlignment="Bottom" Width="25" Grid.Column="2" Grid.Row="7" />
            <Label Content="*" Foreground="Red" Height="25" HorizontalAlignment="Right" Margin="0,0,142,27" Name="label6" VerticalAlignment="Bottom" Width="49" Grid.Column="2" Grid.ColumnSpan="2" Grid.Row="7" Grid.RowSpan="2" />
        </Grid>
            </Grid>

</Window>

1 个答案:

答案 0 :(得分:0)

在内部网格中,您可以将选定项目的转换器应用于可见性。

在XAML中添加此资源:

<local:NotNullToVisibilityConverter  x:Key="NotNullToVisibilityConverter" />

并在您的孩子Grid中使用它,如下所示:

<Grid Grid.Row="1" Visibility={Binding SelectedEmployee, Converter={StaticResource NotNullToVisibilityConverter}} ...>

这是转换器代码:

public class NotNullToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return Visibility.Hidden; // or Visibility.Collapsed

            return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }