2012-06-17 50 views
2

我正在嘗試將WPF XAML中按鈕的數據模板更改爲c#代碼I.E.我想在.cs文件中以編程方式創建按鈕。將XAML更改爲C#代碼

的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 [email protected], Path=Value}" Margin="5" Padding="5" HorizontalContentAlignment="Center" Width="{Binding [email protected]}" Height="{Binding [email protected]}"/> 
      </DataTemplate> 
      </Setter.Value> 
     </Setter> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 

</ItemsControl> 

行:

<Button Command="Open" Tag="{Binding [email protected], Path=Value}" Margin="5" Padding="5" HorizontalContentAlignment="Center" Width="{Binding [email protected]}" Height="{Binding [email protected]}"/> 

是我綁定所有的按鈕到一個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> 

但我想要的是,而不是調用所有的底部預定義的XML文件,用按鈕調用CS中的方法,像這個:

public void createButtons() 
{ 
    Button button = new Button(); 
    button.Tag = "I am from cs"; 
    buttons.Items.Add(button); 
} 

這可能嗎?

+0

究竟對我們的'createButtons()'方法不起作用?一般來說,XAML中可能的所有內容在C#代碼中也是可能的。 –

+0

你爲什麼想這樣做?使用XAML有什麼問題? –

+0

是的,這是可能的。你有什麼嘗試?如果您在編碼時存在一些問題,請諮詢相關問題。如果你想在某個地方開始,看看'button.ItemContainerStyle'和'buttons.ItemsPanel',看看它們的類型,然後從那裏開始。這對我來說並沒有太大的意義,爲什麼你想這樣做呢。如果你解釋爲什麼你想這樣做,有人可能會提出一個更好的方法。 –

回答

0

你不需要C#本身就有動態按鈕。

我已經在生產中的應用,具有動態按鈕(你可以有不同的按鈕添加更多等,都在代碼和他們只是顯示在WPF UI)

你只需要創建一個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 
    } 
} 

創建列表像這樣在你的代碼:

List<ButtonViewModel> buttonList = new List<ButtonViewModel>(); 
ButtonViewModel bvm1 = new ButtonViewModel(); 
bvm.ButtonCommand = new RelayCommand(f => SomeMethod()); 
buttonList.add(bvm1); 

注:MVVM Base.zip

然後使用一個ItemsControl其的ItemsSource綁定到:您可以從任何MVVM框架或在這裏得到RelayCommand你的buttonList,它會正確顯示列表。