动态更改 wpf 中网格的内容 [英] changing contents of a grid in wpf dynamically

查看:19
本文介绍了动态更改 wpf 中网格的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 usercontrol 动态更改网格的内容.我的 mainWindow.xaml 看起来像这样.

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

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

在单击 button1 时,我试图更改名为 Grid1 的网格的内容.我用简单的 textblock 创建了一个 UserControl .

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

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

我的 mainwindow.cs 看起来像这样

My mainwindow.cs looks like this

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.

推荐答案

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

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


    }

这篇关于动态更改 wpf 中网格的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆