动态更改 wpf 中网格的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26478742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:38:06  来源:igfitidea点击:

changing contents of a grid in wpf dynamically

c#wpfxamluser-controls

提问by user3438081

I am trying to change the contents of a grid dynamically using usercontrol.My mainWindow.xaml looks like this.

我正在尝试使用 usercontrol.My 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>

On clicking button1 one I am trying to change the content of the Grid named Grid1. I created a UserControl with simple textblock .

单击 button1 时,我试图更改名为 Grid1 的网格的内容。我用简单的 textblock 创建了一个 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>

My mainwindow.cs looks like this

我的 mainwindow.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; }

}
}

on clicking button the content is not changing can anyone help please! Thanks in advance.

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

回答by jadavparesh06

You will need to first clear the grid children and then add the user control within your grid.

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

 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);


    }