实现一个切换配方的功能

代码如下

public class Student : BindableBase
{
    private string _title;

    public string Title
    {
        get => _title;
        set => SetProperty(ref _title, value);
    }

    private string _name;

    public string Name
    {
        get => _name;
        set => SetProperty(ref _name, value);
    }
    private string _password;

    public string Password
    {
        get => _password;
        set => SetProperty(ref _password, value);
    }
}

public class StudentList : ObservableCollection<Student>
{
    public StudentList()
    {
        Add(
            new Student()
            {
                Title = "标题1",
                Name = "123",
                Password = "123"
            }
        );
        Add(
            new Student()
            {
                Title = "标题2",
                Name = "456",
                Password = "456"
            }
        );
        Add(
            new Student()
            {
                Title = "标题3",
                Name = "789",
                Password = "789"
            }
        );
    }
}

 <Window.Resources>
     <Style TargetType="{x:Type Button}">
         <Setter Property="Background" Value="Transparent" />

         <Setter Property="Padding" Value="0" />
         <Setter Property="Template">
             <Setter.Value>
                 <ControlTemplate TargetType="Button">
                     <Border Background="{TemplateBinding Background}">
                         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                     </Border>
                     <ControlTemplate.Triggers>
                         <Trigger Property="IsPressed" Value="True">
                             <!--  定义按钮被按下时的样式  -->
                             <Setter Property="Background" Value="Aqua" />
                         </Trigger>
                     </ControlTemplate.Triggers>
                 </ControlTemplate>
             </Setter.Value>
         </Setter>
     </Style>
     <Style TargetType="{x:Type ListBox}">
         <Setter Property="Background" Value="Transparent" />
         <Setter Property="BorderBrush" Value="Transparent" />
         <Setter Property="BorderThickness" Value="0" />
     </Style>
     <ObjectDataProvider x:Key="MyStudent" ObjectType="{x:Type local:StudentList}" />
     <!--<DataTemplate DataType="{x:Type local:Student}">
         <Button
             Width="100"
             Command="{Binding DataContext.SelectedViewCommand, ElementName=win}"
             Content="{Binding Title}" />
     </DataTemplate>-->
     <DataTemplate x:Key="DetailTemplate">
         <Border
             Width="300"
             Height="100"
             Margin="20"
             Padding="8"
             BorderBrush="Aqua"
             BorderThickness="1">
             <Grid>
                 <Grid.RowDefinitions>
                     <RowDefinition />
                     <RowDefinition />
                     <RowDefinition />
                 </Grid.RowDefinitions>
                 <Grid.ColumnDefinitions>
                     <ColumnDefinition />
                     <ColumnDefinition />
                 </Grid.ColumnDefinitions>
                 <TextBlock
                     Grid.Row="0"
                     Grid.Column="0"
                     Text="Title:" />
                 <TextBlock
                     Grid.Row="0"
                     Grid.Column="1"
                     Text="{Binding Path=Title}" />
                 <TextBlock
                     Grid.Row="1"
                     Grid.Column="0"
                     Text="Name:" />
                 <TextBlock
                     Grid.Row="1"
                     Grid.Column="1"
                     Text="{Binding Path=Name}" />
                 <TextBlock
                     Grid.Row="2"
                     Grid.Column="0"
                     Text="Password:" />
                 <TextBlock
                     Grid.Row="2"
                     Grid.Column="1"
                     Text="{Binding Path=Password}" />
             </Grid>
         </Border>
     </DataTemplate>
 </Window.Resources>
 <Grid>
     <Grid.RowDefinitions>
         <RowDefinition Height="40" />
         <RowDefinition />
     </Grid.RowDefinitions>

     <ListBox
         Margin="10,0,10,0" IsSynchronizedWithCurrentItem="True" ScrollViewer.VerticalScrollBarVisibility="Disabled"
         ItemsSource="{Binding Source={StaticResource MyStudent}}"
         SelectedIndex="0"
         SelectionMode="Single">
         <ListBox.ItemsPanel>
             <ItemsPanelTemplate>
                 <StackPanel Orientation="Horizontal" />
             </ItemsPanelTemplate>
         </ListBox.ItemsPanel>
         <ListBox.ItemTemplate>
             <DataTemplate>
                 <TextBlock Margin="10" FontSize="16" FontWeight="Bold" FontFamily="楷体" Text="{Binding Title}" />
             </DataTemplate>
         </ListBox.ItemTemplate>
         <ListBox.ItemContainerStyle>
             <Style TargetType="ListBoxItem">
                 <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                 <Setter Property="BorderThickness" Value="0" />
                 <Setter Property="Background" Value="Transparent" />
                 <Setter Property="Template">
                     <Setter.Value>
                         <ControlTemplate TargetType="{x:Type ListBoxItem}">
                             <Grid>
                                 <Border Name="border" />
                                 <ContentPresenter />
                             </Grid>
                             <ControlTemplate.Triggers>
                                 <Trigger Property="IsSelected" Value="True">
                                     <Setter TargetName="border" Property="Background" Value="Transparent" />
                                 </Trigger>
                             </ControlTemplate.Triggers>
                         </ControlTemplate>
                     </Setter.Value>
                 </Setter>
             </Style>
         </ListBox.ItemContainerStyle>
     </ListBox>

     <ContentControl Grid.Row="1" Content="{Binding Source={StaticResource MyStudent}}"
         ContentTemplate="{StaticResource DetailTemplate}"/>
 </Grid>
posted @ 2024-08-30 01:04  孤沉  阅读(7)  评论(0编辑  收藏  举报