动态更改wpf中网格的内容

本文关键字:网格 中网 wpf 动态 | 更新日期: 2023-09-27 18:27:11

我正在尝试使用usercontrol动态更改网格的内容。我的mainWindow.xaml是这样的。

<Window x:Class="testapp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="524" Width="856">
<Grid Background="Black">
    <!--<Image Height="44" HorizontalAlignment="Left" Margin="284,0,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="229" Source="/testapp;component/Images/Picture1.png" />-->
    <Grid Height="431" HorizontalAlignment="Left" Margin="0,55,0,0" Name="grid1" VerticalAlignment="Top" Width="266" Background="Black" Opacity="0.4">
        <ListView  Height="430" HorizontalAlignment="Left" Margin="3,1,0,0" x:Name="listView1" VerticalAlignment="Top" Width="260" Background="Black">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical" Height="70">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Name : " Foreground="Yellow" />
                            <TextBlock Text="{Binding Name}" Foreground="Yellow" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Height="100">
                            <TextBlock Text="Source :" Foreground="Yellow"/>
                            <TextBlock Text="{Binding Source}" Foreground="Yellow"/>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button Content="create new Group" Height="36" HorizontalAlignment="Left" Margin="6,392,0,0" Name="button1" VerticalAlignment="Top" Width="254" Click="button1_Click" />
        <ContentControl x:Name="devlist"/>
    </Grid>
</Grid>

单击按钮1,我试图更改名为Grid1的网格的内容。我用简单的文本块创建了一个UserControl。

<UserControl x:Class="testapp.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
    <TextBlock  Height="23" HorizontalAlignment="Left" Margin="80,122,0,0" Name="textBlock1" Text="hello" VerticalAlignment="Top" />
</StackPanel>

我的主窗口.cs看起来像这个

namespace testapp
{
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
         Items = new List<MyItems>();
        Items.Add(new MyItems() { Name = "House Party", Source = " DLNA" });
        Items.Add(new MyItems() { Name = "Outdoor Party", Source = " DLNA" });
        listView1.ItemsSource = Items;
    }
    public List<MyItems> Items { get; set; }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        this.devlist.Content = new UserControl1();
    }
}
public class MyItems
{
    public String Name { get; set; }
    public String Source { get; set; }
}
}

点击按钮,内容不会改变,请任何人帮忙!提前谢谢。

动态更改wpf中网格的内容

您需要首先清除网格子项,然后在网格中添加用户控件。

 private void button1_Click(object sender, RoutedEventArgs e)
    {
// first remove the existing content within grid
grid1.Children.Clear();
// then add your user contrl here
testapp.UserControl1 usercontrol = new testapp.UserControl1();
grd1.Children.Add(usercontrol);

    }