2014-03-05 27 views
2

我需要創建一個簡單的CRUD,並且我想在不同的頁面中重複使用相同的「模板」。創建一個自定義模板作爲ResourceDictionary並從代碼隱藏中訪問它

MyCrud.xaml(模板)

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

<!-- disegno la mia crud--> 
<DataTemplate x:Key="MyCrud"> 
    <StackPanel> 
     <StackPanel Orientation="Horizontal" Margin="0,20,0,20"> 
      <TextBlock Text="Code" FontWeight="Bold" Width="150" FontSize="24"/> 
      <TextBox x:Name="edtCode" InputScope="Number" Width="300" Margin="20,0,20,0" HorizontalAlignment="Left"/> 
     </StackPanel> 
     <StackPanel Orientation="Horizontal" Margin="0,20,0,20"> 
      <TextBlock Text="Vin" FontWeight="Bold" Width="150" FontSize="24"/> 
      <TextBox x:Name="edtVin" Width="300" Margin="20,0,20,0" HorizontalAlignment="Left"/> 
     </StackPanel> 
     <StackPanel Orientation="Horizontal" Margin="0,20,0,20"> 
      <TextBlock Text="Url" FontWeight="Bold" Width="150" FontSize="24"/> 
      <TextBox x:Name="edtUrl" Width="300" Margin="20,0,20,0" HorizontalAlignment="Left"/> 
     </StackPanel> 
    </StackPanel> 
</DataTemplate> 

然後我在App.xaml中註冊它

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="View/MyCrud.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

所以現在我希望把它放在一個頁面,在我的SearchPage.xaml中使用它

SearchPage.xaml

<Page ...bla bla bla> 
<ContentControl x:Name="MyTemplate" Content="{Binding}" ContentTemplate="{StaticResource MyCrud}" /> 
</Page> 

目前的佈局完美的作品

的問題是:

  1. 我如何可以訪問TextBox?例如,從SearchPage的代碼隱藏名爲 edtVin
  2. 爲什麼TextBox p= GetTemplateChild("edtVin") as TextBox;總是空?
  3. 我發現這個solution,這是最好的嗎?

謝謝!

回答

1

1)如何訪問TextBox,例如從SearchPage的代碼隱藏中調用 「edtVin」?

你可以得到這樣的:

MyTemplate.ContentTemplate.FindName("edtVin", MyTemplate) as TextBox; 

2)爲什麼文本框P = GetTemplateChild( 「edtVin」)的文本框;

由於edtVin是ContentControl的模板子,而不是Page。

3)我發現該解決方案,它是最好的嗎?

是的,你也可以使用VisualTreeHelper方法。將它們作爲工具函數添加到項目中,以便可以從代碼中的任何位置訪問它。

+0

'MyTemplate.ContentTemplate'沒有任何方法,比如'FindName'或者Find-something – ArghArgh

+0

你在說'WPF'嗎?對?方法確實存在,你可以看到[這裏](http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname(v = vs.110).aspx)。在我的最後編譯罰款。 –

+0

我正在嘗試創建一個Windows應用商店應用,之前我選擇了錯誤的標記 – ArghArgh

2

「創建適當的視圖模型,並使用適當的數據綁定和所有 問題會奇蹟般地消失了。」

DataTemplate旨在被用作一個「某數據元素的可視化表示」。

不需要來「訪問」WPF程序代碼中的任何UI元素。無論修改這些元件中的任何性能應採用合適的DataBinding來完成,否則,如果您手動修改UI元素的屬性(而不修改對應底層數據項),你基本上打破UI和數據之間的一致性,並且DataTemplate中的UI元素將不再反映數據項目中的數據。

創建一個簡單的類,它表示要在屏幕上顯示的數據:

public class MyData 
{ 
    public string Code {get;set;} 

    public string Vin {get;set;} 

    public string Url {get;set;} 
} 

然後,爲了支持雙向數據綁定WPF,有你Implement the INotifyPropertyChanged interface類。

然後,設定UI的DataContext這些項目的相關實例(或集合):

public MyWindow() //Window's constructor 
{ 
    InitializeComponent(); 

    var list = new List<MyData>(); 

    //... populate the list with data here... 

    DataContext = list; 
} 

,並同時保留UI然後簡單地操縱這些數據項的屬性:

var item = list[0]; //for example 

item.Code = "My New Code"; 

這是使用WPF的正確方法。

相關問題