C# 通过单击按钮更改窗口内容
即使有其他答案,我也会发布这篇文章,因为我尝试了很多解决方案,但没有找到一个适合我的问题 我做了一些截图来更好地解释我自己 我有一个如下所示的应用程序: A、 B、C、D和灰色按钮都是简单的按钮 如果我单击一个按钮,我希望内容发生更改,如下所示: 如果我点击B、C、D也一样,但内容会有所不同 更深入地说,我希望同样的事情发生在按钮E,F,G,H和更多的深度。(所有应用程序均基于4按钮设计) 到目前为止,我使用以下XAML代码构建了应用程序主窗口的设计:C# 通过单击按钮更改窗口内容,c#,wpf,dynamic,window,C#,Wpf,Dynamic,Window,即使有其他答案,我也会发布这篇文章,因为我尝试了很多解决方案,但没有找到一个适合我的问题 我做了一些截图来更好地解释我自己 我有一个如下所示的应用程序: A、 B、C、D和灰色按钮都是简单的按钮 如果我单击一个按钮,我希望内容发生更改,如下所示: 如果我点击B、C、D也一样,但内容会有所不同 更深入地说,我希望同样的事情发生在按钮E,F,G,H和更多的深度。(所有应用程序均基于4按钮设计) 到目前为止,我使用以下XAML代码构建了应用程序主窗口的设计: <Window x:Class=
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Name="RootWindow" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition Height="10"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Button Name="Audiovisiva" Grid.Row="0" Grid.Column="0" Background="Yellow" FontWeight="Bold" FontSize="24" Foreground="#FF7F00FF" >A</Button>
<Button Name="Orientamento" Grid.Row="0" Grid.Column="1" Background="Red" Foreground="#FF00FF15" FontSize="24" FontWeight="Bold">B</Button>
<Button Name="Button3" Grid.Row="1" Grid.Column="0" Background="#FFB900FF" Foreground="#FFFBFF00" FontSize="24" FontWeight="Bold">C</Button>
<Button Name="Button4" Grid.Row="1" Grid.Column="1" Background="#FF00EBFF" Foreground="#FFFFC902" FontSize="24" FontWeight="Bold">D</Button>
<Button Name="Button5" Grid.Row="4" Grid.Column="0">Back</Button>
<Button Name="Button6" Grid.Row="4" Grid.Column="1">Next</Button>
</Grid>
</Window>
A.
B
C
D
返回
下一个
我的问题是:在这种情况下,有没有一种聪明的方式来显示动态内容?(我是WPF编程的新手,请尽可能地解释我)
记住,会有很多可能的层,因为每个按钮都必须“生成”一组新的4个按钮,每次深入应用程序内部
谢谢你能给我的任何帮助
编辑:
不,一旦我点击“A”,没有一个“A”会重复。主要目标是创建一个套件,您可以在其中选择a、B、C、D之间的选项,如果您得到“a”,下一个视图将显示4个按钮,以便在“a”功能的不同选项之间进行选择。举个例子,假设我选择“阅读”功能,在下一个视图中,我可以选择“开始”、“选项”、“某物”、“主页”。如果我按“开始”,我想启动我的应用程序核心(在本例中,是一个阅读练习),如果我按“选项”,我想在“阅读速度”、“字体大小”ecc。。。要设置我的阅读练习选项,如果按“something”,其他操作将完成,如果按“Home”,我想返回第一个屏幕
主要目的是在4个按钮屏幕之间导航以设置选项,然后按“开始”按钮开始个性化练习
我希望我能很好地解释清楚。你想展示什么样的新功能 您只需将ButtonClickeEvents添加到每个方块中的按钮。 例如: 按钮名称=“Audiovisiva”Grid.Row=“0”Grid.Column=“0”Background=“Yellow”fontwweight=“Bold”FontSize=“24”前台=“#FF7F00FF”按钮。单击按钮 然后 私有无效按钮1\u单击(对象发送者,事件参数e) { //在这里行动 } 我认为,在网格的顶部,您可以放置4个画布,您可以轻松地更改内容。
像canvas.content=“image”一样,这里有一个工作示例,其中包含您发布/编辑的信息和一点
数据绑定
首先创建一个类项,a)表示按钮b)保存按钮信息,c)是复合模式
所以项目
可以包含项目
public class Item
{
public Item(string letter, Brush back, Brush front)
{
Letter = letter;
BackColor = back;
FrontColor = front;
}
public Item(string letter, Brush back, Brush front, List<Item> items)
: this(letter, back, front)
{
Items = items;
}
public List<Item> Items { get; set; }
public string Letter { get; set; }
public Brush BackColor { get; set; }
public Brush FrontColor { get; set; }
}
公共类项目
{
公共项目(字符串字母、刷背、刷前)
{
字母=字母;
背色=背色;
FrontColor=front;
}
公共项目(字符串字母、刷背、刷前、列表项目)
:此(字母、背面、正面)
{
项目=项目;
}
公共列表项{get;set;}
公共字符串字母{get;set;}
公共画笔背景色{get;set;}
公共笔刷FrontColor{get;set;}
}
这是MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfApplication1="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<Grid.Resources>
<DataTemplate DataType="{x:Type wpfApplication1:Item}">
<Button Content="{Binding Letter}" Foreground="{Binding FrontColor}" Background="{Binding BackColor}" Click="Button_OnClick" Tag="{Binding}"/>
</DataTemplate>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition Height="10"/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<ContentPresenter Grid.Row="0" Grid.Column="0" Content="{Binding Items[0]}"/>
<ContentPresenter Grid.Row="0" Grid.Column="1" Content="{Binding Items[1]}"/>
<ContentPresenter Grid.Row="1" Grid.Column="0" Content="{Binding Items[2]}"/>
<ContentPresenter Grid.Row="1" Grid.Column="1" Content="{Binding Items[3]}"/>
<Button Name="Button5" Grid.Row="4" Grid.Column="0">Back</Button>
<Button Name="Button6" Grid.Row="4" Grid.Column="1">Next</Button>
</Grid>
返回
下一个
这是MainWindow.cs
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
Items = BuildItems();
InitializeComponent();
}
private List<Item> BuildItems()
{
var list = new List<Item>();
// Add option "A" with suboptions
list.Add(new Item("A", Brushes.Red, Brushes.Yellow, new List<Item>
{
new Item("E", Brushes.Green, Brushes.Black),
new Item("F", Brushes.LightBlue, Brushes.Black),
new Item("G", Brushes.Red, Brushes.Black),
new Item("H", Brushes.LemonChiffon, Brushes.Black)
}));
// Add option "B"
list.Add(new Item("B", Brushes.Yellow, Brushes.Green));
// Add option "C"
list.Add(new Item("C", Brushes.Blue, Brushes.Yellow));
// Add option "D"
list.Add(new Item("D", Brushes.GreenYellow, Brushes.Yellow));
return list;
}
private List<Item> item;
public List<Item> Items
{
get { return item; }
set
{
if (item != value)
{
item = value;
OnPropertyChanged("Items");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
private void Button_OnClick(object sender, RoutedEventArgs e)
{
var item = (sender as Button).Tag as Item;
if (item != null && item.Items != null)
Items = item.Items;
}
}
//
///Interaktionslogik für MainWindow.xaml
///
公共部分类主窗口:窗口,INotifyPropertyChanged
{
公共主窗口()
{
Items=BuildItems();
初始化组件();
}
私有列表BuildItems()
{
var list=新列表();
//添加带有子选项的选项“A”
列表。添加(新项目(“A”),画笔。红色,画笔。黄色,新列表
{
新项目(“E”,刷子。绿色,刷子。黑色),
新项目(“F”,刷子。浅蓝色,刷子。黑色),
新项目(“G”,刷子。红色,刷子。黑色),
新项目(“H”,刷子。LemonChiffon,刷子。黑色)
}));
//添加选项“B”
列表。添加(新项目(“B”,画笔。黄色,画笔。绿色));
//添加选项“C”
列表。添加(新项目(“C”,画笔。蓝色,画笔。黄色));
//添加选项“D”
列表。添加(新项目(“D”,画笔。绿黄色,画笔。黄色));
退货清单;
}
私人清单项目;
公共清单项目
{
获取{return item;}
设置
{
如果(项目!=值)
{
项目=价值;
不动产变更(“项目”);
}
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged(字符串propertyName)
{
var handler=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
私有无效按钮\u OnClick(对象发送器,路由目标)
{
变量项=(发送者为按钮)。标记为项;
if(item!=null&&item.Items!=null)
项目=项目。项目;
}
}
这个例子应该是开箱即用的。要自定义,请按照中的“我的代码”
列出BuildItems()
。在这一点上,它对您的需求非常灵活。但是,有了更深入的wpf知识,代码会有一些改进(使用ICommand、ViewModels等)
希望我能提供帮助按钮是否会在过程中重复?例如,点击后是否有另一个“按钮A”