我正在爲MyObject
創建DataTemplate
。例如,MyObject
包含StackPanel
,其名稱爲public StackPanel MyStackPanel
。我如何將MyStackPanel
插入到MyObject的DataTemplate中?DataTemplate插入UserControl
0
A
回答
1
這可以做到,但我不明白你爲什麼想要。
在這個例子中,我使用「Customer」作爲對象類型,並在其中包含一個Button(但它可以很容易地是一個StackPanel)。
public class Customer : DependencyObject
{
public Customer()
{
MyButton = new Button();
MyButton.Content = "I'm a button!";
}
#region MyButton
public Button MyButton
{
get { return (Button)GetValue(MyButtonProperty); }
set { SetValue(MyButtonProperty, value); }
}
public static readonly DependencyProperty MyButtonProperty =
DependencyProperty.Register("MyButton", typeof(Button), typeof(Customer));
#endregion
}
我不知道,如果你能做到這一點不使你的對象爲DependencyObject和定義嵌套控制作爲一個依賴屬性。實現INotifyPropertyChanged可能會作爲替代(如果您的對象不能從DependencyObject繼承),但我沒有測試過。
與模板的主窗口:
<Window x:Class="TemplateTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TemplateTest"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Customer}">
<ContentPresenter Content="{Binding MyButton}" />
</DataTemplate>
</Window.Resources>
<Grid>
<ItemsControl x:Name="CustomersList" />
</Grid>
</Window>
正如你所看到的,我用一個ContentPresenter綁定從物體發出的按鈕。
然後,您可以使用此測試:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += (s, e) =>
{
var myCustomer1 = new Customer();
var myCustomer2 = new Customer();
var customers = new ObservableCollection<Customer>();
customers.Add(myCustomer1);
customers.Add(myCustomer2);
CustomersList.ItemsSource = customers;
};
}
}
0
你不能這樣做,模板正在構建被執行的計劃,它們不包含特定實例或對特定實例的引用。
相關問題
- 1. WPF,UserControl或DataTemplate
- 2. 在UserControl中實現DataTemplate DependencyProperty
- 3. WPF設計UserControl/DataTemplate問題
- 4. 從DataTemplate UWP綁定UserControl DP
- 5. XML DataTemplate綁定多層UserControl
- 6. 在FlowLayoutPanel中插入Usercontrol
- 7. UWP將ListView的項綁定爲UserControl DataTemplate
- 8. 用DataTemplate創建的UserControl處理問題
- 9. MVVM uwp UserControl與VM作爲DataTemplate
- 10. 如何從UserControl實例創建DataTemplate?
- 11. WPF:如何動態查找usercontrol或datatemplate
- 12. 在UserControl的DataTemplate中指定Converter
- 13. UWP DataContext從DataTempalte到DataTemplate中的UserControl
- 14. 以編程方式將DataTemplate插入另一個DataTemplate
- 15. 在UserControl中插入一個控件
- 16. datatemplate中的usercontrol和「主」網格之間的區別
- 17. 使用UserControl作爲DataTemplate(與幾個綁定)在列表視圖
- 18. 帶有usercontrol的WPF列表框作爲ItemTemplate DataTemplate綁定問題
- 19. 通過DataTemplate顯示UserControl的不同實例
- 20. 內容控制+ DataTemplate動態改變UserControl DevExpress wpf
- 21. 我的UserControl在DataTemplate中使用時未呈現
- 22. 從WPF UserControl的<DataTemplate>中訪問VM
- 23. 將數據綁定到由DataTemplate承載的UserControl
- 24. 顯示/隱藏WPF中的UserControl的DataTemplate中的控件
- 25. 爲什麼在DataTemplate中使用UserControl比直接使用xaml慢?
- 26. 將DataTemplate的datacontext屬性綁定到usercontrol依賴屬性
- 27. UserControl插件使用System.Add
- 28. 依賴注入UserControl
- 29. 將ContentControl *放入WPF DataTemplate中?
- 30. 如何在asp.net中的Accordion中插入UserControl?
嗯,的DataTemplates通常用於您的控件使用的消費者。你想創建一種...默認模板? – 2011-12-17 18:28:31