[英]Change XAML to c# code
I am trying to change a data template of buttons from WPF XAML to c# code IE I want to create the buttons programatically in a .cs file. 我正在尝试将按钮的数据模板从WPF XAML更改为c#代码IE,我想以编程方式在.cs文件中创建按钮。
The XAML looks like this: XAML看起来像这样:
<ItemsControl DataContext="{Binding Source={StaticResource Things}}" ItemsSource="{Binding}" Name="buttons" Margin="0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<local:FishEyePanel ScaleToFit="false"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Button Command="Open" Tag="{Binding XPath=@Tag, Path=Value}" Margin="5" Padding="5" HorizontalContentAlignment="Center" Width="{Binding XPath=@Width}" Height="{Binding XPath=@Height}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
The line: 该行:
<Button Command="Open" Tag="{Binding XPath=@Tag, Path=Value}" Margin="5" Padding="5" HorizontalContentAlignment="Center" Width="{Binding XPath=@Width}" Height="{Binding XPath=@Height}"/>
is where I bind all the buttons to a xml file, here is the file: 是将所有按钮绑定到xml文件的位置,这里是文件:
<XmlDataProvider x:Key="Things" XPath="Things/Thing">
<x:XData>
<Things xmlns="">
<Thing Button="uno1" Tag="abc" Width="200" Height="200"/>
</Things>
</x:XData>
</XmlDataProvider>
But what I want is instead of calling the xml file with all the bottoms predefined, call a method in cs with the buttons, something like this: 但是我想要的不是代替使用所有预定义的底部调用xml文件,而是使用按钮在cs中调用方法,如下所示:
public void createButtons()
{
Button button = new Button();
button.Tag = "I am from cs";
buttons.Items.Add(button);
}
Is this possible? 这可能吗?
You don't need C# to have dynamic buttons per se. 您不需要C#本身就具有动态按钮。
I have an app in production that has dynamic buttons (you can have different buttons add more, etc, all in code and they are just displayed in the WPF UI) 我在生产中有一个带有动态按钮的应用程序(您可以让不同的按钮添加更多内容,等等,所有内容都在代码中,它们仅显示在WPF UI中)
You just need to create a ButtonViewModel object and then put a list of ButtonViewModel in your code. 您只需要创建一个ButtonViewModel对象,然后在代码中放入一个ButtonViewModel列表即可。
using System;
using System.Windows.Input;
using Setup.Common;
namespace ExampleOfButtonList
{
public class ButtonViewModel : ViewModelBase
{
#region Member fields
protected bool _IsVisible;
protected bool _IsEnabled;
protected RelayCommand _ButtonCommand;
protected String _Text;
#endregion
#region Constructors
/// <summary>
/// The default constructor
/// </summary>
public ButtonViewModel()
{
}
#endregion
#region Properties
public virtual ICommand ButtonCommand
{
get { return _ButtonCommand; }
set
{
_ButtonCommand = value as RelayCommand;
NotifyPropertyChanged("ButtonCommand");
}
}
public bool IsVisible
{
get { return _IsVisible; }
set
{
_IsVisible = value;
NotifyPropertyChanged("IsVisible");
}
}
public String Text
{
get { return _Text; }
set
{
_Text = value;
NotifyPropertyChanged("Text");
}
}
#endregion
}
}
Create the list like this in your code: 在代码中创建如下列表:
List<ButtonViewModel> buttonList = new List<ButtonViewModel>();
ButtonViewModel bvm1 = new ButtonViewModel();
bvm.ButtonCommand = new RelayCommand(f => SomeMethod());
buttonList.add(bvm1);
Note: You can get RelayCommand from any MVVM framework or here: MVVM Base.zip 注意:您可以从任何MVVM框架或此处获取RelayCommand: MVVM Base.zip
Then use an ItemsControl and bind its ItemsSource to your buttonList and it will properly display the list. 然后使用ItemsControl并将其ItemsSource绑定到buttonList,它将正确显示该列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.