2016-06-07 145 views
0

我試圖動態地創建一些XAML控件。我想我知道我怎麼可以自己添加「東西」,但我不知道如何將它添加到指定的位置。如何指定在c#中添加xaml控件的位置?

這裏是XAML -file(toolStencils.xaml)的一個樣本:

<LinearGradientBrush x:Key="colorTemplate" StartPoint="0.6,0" EndPoint="0,1" Opacity="1"> 
    <LinearGradientBrush.GradientStops> 
     <GradientStop Color="Black" Offset="0.01" /> 
     <GradientStop Color="Green" Offset="0.5" /> 
     <GradientStop Color="Blue" Offset="1" /> 
    </LinearGradientBrush.GradientStops> 
</LinearGradientBrush> 

<!-- ToolTemplate --> 
     <Grid IsHitTestVisible="False" ToolTip="Tool 1"> 
      <Path Stretch="Fill" IsHitTestVisible="False" 
        StrokeLineJoin="Round" 
        Fill="{StaticResource colorTemplate}" 
        Stroke="Black" 
        StrokeThickness="0.2" 
        Data="M 0,0 H60 V60 H0 Z"> 
       <s:ToolItem.Template> 
        <ControlTemplate> 
         <Path Fill="Transparent" Stretch="Fill" Data="M 0,0 H60 V50 H0 Z"/> 
        </ControlTemplate> 
       </s:ToolItem.Template> 
      </Path> 
      <Grid Margin="2"> 
       <Label VerticalAlignment="Top" 
         HorizontalAlignment="Right" 
         Content="Tool number 1" 
         FontSize="10"> 
        <Label.Resources> 
         <Style TargetType="TextBlock"> 
          <Setter Property="TextWrapping" Value="Wrap"/> 
         </Style> 
        </Label.Resources> 
       </Label> 
      </Grid> 
     </Grid> 


<s:Toolbox x:Key="Tools" ItemSize="80,75"> 
    <ItemsControl.Items x:Uid="itemControls"> 



     <!-- Tool 1 --> 
     <!-- ........... --> 

    </ItemsControl.Items> 
</s:Toolbox> 

現在,我想添加更多的 「工具」 動態。 (worker.cs)

// Load the resource dictionary 

      var rd = new System.Windows.ResourceDictionary(); 
      rd.Source = new System.Uri("pack://application:,,,/MyApplication;component/FirstFolder/SecondFolder/MyXamlFile.xaml", System.UriKind.RelativeOrAbsolute); 

      // 
      if() // Do something 
      { 
       // Build and add same kind of tools than listed in xaml file 
       // Grid grid = new Grid(); 
       // etc... 
      } 
      else 
      { 
       System.Windows.MessageBox.Show("Couldn't add tools."); 
      } 

如何添加所有<ItemsControl.Items>裏面的東西?然後,如果我添加Grid,例如,如何定義,添加哪些內容以及哪些內容不是?

我看了,並嘗試例如WPF: Add controls from codeAdding WPF Controls

問題是,我分開.xaml文件和.cs文件。所以我不能直接引用名稱或鍵。

我希望你能稍微得到我的觀點。

提前致謝!

+1

只需使用模板。不要浪費你的時間來對抗框架。 –

回答

0

問題是,我分離了.xaml文件和.cs文件。所以我不能直接引用名稱或鍵。

您顯然需要獲取對視圖或某些共享項目(視圖模型)的引用,否則您將無法完成此操作。你

<ItemsControl.Items x:Uid="itemControls" ItemsSource="{Binding DynamicControls}"> 

在你的工人將需要通過:

我建議你改變你的ItemsContainer綁定到的視圖模型中指定控制列表(我假設你有一個)參照視圖模型,然後在代碼中,你可以這樣做:

 if() // Do something 
     { 
      Grid grid = new Grid(); 
      // populate grid 
      myViewModel.DynamicControls.Add(grid); 
     } 

您需要確保屬性更改通知時,將項目添加使視圖刷新升高。這樣做的最好方法是使DynamicControls屬性成爲可觀察集合。

這是一個工作的髒樣本,我直接在視圖背面製作,所以你可以看到它的工作。

查看:

<Window x:Class="WPFDynamicControls.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WPFDynamicControls" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525" > 
    <StackPanel> 
     <Label>My Application</Label> 
     <Button Click="ButtonBase_OnClick">Add Control</Button> 
     <ItemsControl ItemsSource="{Binding MyControls}"> 

     </ItemsControl> 
    </StackPanel> 
</Window> 

後面的代碼:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     this.MyControls = new ObservableCollection<Control>(); 
     this.DataContext = this; 
     InitializeComponent(); 
    } 

    public ObservableCollection<Control> MyControls { get; set; } 

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e) 
    { 
     this.MyControls.Add(new Button() { Content = "New Button" }); 
    } 
} 

希望幫助!

+0

我明白這一點並不清楚,那些控件來自哪裏。我有一個xml文件,我首先閱讀它,並基於此,我想創建「工具」。所以基本上,無論何時從xml文件中找到「工具」,它都將被創建爲xaml -file。這就是我要找的。 –

+0

我不太明白你的意思。所以我應該首先創建一個我想添加的「工具」列表。然後將該列表綁定到ItemsControl.Items? –

+0

是的,如果你在綁定之前創建列表,並且不希望它發生變化,那麼你可以很懶,不用擔心更改通知。 – Jynx

相關問題