问题
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>
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>
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.
回答1:
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);
}
来源:https://stackoverflow.com/questions/26478742/changing-contents-of-a-grid-in-wpf-dynamically