【发布时间】:2014-10-21 04:08:38
【问题描述】:
我正在尝试使用 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>
单击 button1 时,我试图更改名为 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>
我的 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; }
}
}
点击按钮后内容没有改变,谁能帮忙!提前致谢。
【问题讨论】:
-
根据您的问题,您想更改 Grid1 的内容,但是您正在从代码中设置 devlist 的内容,即 ContentControl,您能告诉我们您真正想要做什么吗?
-
我想在点击 button1 时更改 grid1 的内容
标签: c# wpf xaml user-controls