2016-08-03 99 views
0

我想將DataTemplate應用於我自動生成的列。我可以用一個簡單的模板來做到這一點。不過,我想要一個組合框與綁定到數據網格的文本和一個橢圓形通過轉換器顯示基於此值的顏色。Datagrid自動生成的列自定義模板綁定

我試過兩種方法。首先在代碼隱藏中創建模板並通過xamlReader加載模板。

private void LeftPanel_AutoGeneratingColumn(
    object sender, DataGridAutoGeneratingColumnEventArgs e) 
    { 
     DataGridTemplateColumn col = new DataGridTemplateColumn(); 
     col.Header = e.Column.Header; 

     string xaml = 
      "<DataTemplate xmlns:local=\"clr-namespace:myView\">" + 
      "<DataTemplate.Resources><local:goColorConverter x:Key=\"goColorConverter\" /></DataTemplate.Resources>" + 
      "<ComboBox " + 
      "SelectedValue=\"{Binding [" + e.Column.Header + 
       "], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}\" Width=\"85\" " + 
      "ItemsSource=\"{Binding Path=DataContext.goNoGo, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}\">" + 
      "<ComboBox.ItemTemplate><DataTemplate><StackPanel Orientation=\"Horizontal\">" + 
      "<Ellipse Fill=\"{Binding Converter={StaticResource goColorConverter}}\" Height=\"14\" Width=\"14\" HorizontalAlignment=\"Right\"/>" + 
      "<TextBlock Text=\"{Binding}\" Padding=\"5 0\"/>" + 
      "</StackPanel></DataTemplate></ComboBox.ItemTemplate></ComboBox></DataTemplate>"; 

     var sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml)); 
     var pc = new ParserContext(); 
     pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"); 
     pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml"); 
     var datatemplate = (DataTemplate)XamlReader.Load(sr, pc); 

     col.CellTemplate = datatemplate; 
     col.CellEditingTemplate = datatemplate; 
     e.Column = col; 

     return; 
    } 

這給了我正確的數據綁定,但我不能用轉換器編譯。我得到的錯誤無法爲我的轉換器創建未知類型(在我的代碼後面居住)。我已經閱讀了許多關於程序集的帖子,但無法用它們來幫助我。

我的第二種方法是在xaml中定義模板並將其應用於我的代碼中。這使轉換器工作,但我不知道如何定義我選定的值綁定,因爲我不知道執行前的列名稱。我希望能夠使用這個Josh smith FindName example,但我不知道如何訪問內容演示者。

代碼:

private void LeftPanel_AutoGeneratingColumn(
    object sender, DataGridAutoGeneratingColumnEventArgs e) 
    { 
     DataGridTemplateColumn col = new DataGridTemplateColumn(); 
     col.Header = e.Column.Header; 

     col.CellTemplate = (DataTemplate)Resources["goDropColumn"]; 
     col.CellEditingTemplate = (DataTemplate)Resources["goDropColumn"]; 

     //Do magic here to get combo box and update its binding 

     e.Column = col; 
     return; 
    } 

XAML:

<DataTemplate x:Key="goDropColumn"> 
    <ComboBox Name="combo" Width="85" ItemsSource="{Binding Path=DataContext.goNoGo, 
     RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
     SelectedValue="{Binding ???}"> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <Ellipse Fill="{Binding Converter={StaticResource goColorConverter}}" 
          Height="14" Width="14"/> 
        <TextBlock Text="{Binding}" Padding="5 0"/> 
       </StackPanel> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
    </ComboBox> 
</DataTemplate> 

我沒有結婚,我的方法和我新的WPF和C#,所以如果該方法是完全錯誤的,請讓我知道。我覺得第二種方法是更清潔,並會工作,如果我可以改變我的綁定標記???到列名稱。

Another possibly relevant article但我沒有自定義數據類型,我只想將selectedValue綁定到列。

,通過機器學習要求更新的代碼:

DataGridTemplateColumn col = new DataGridTemplateColumn(); 
col.Header = e.Column.Header; 


string xaml = 
    @"<DataTemplate x:Key=""goDropColumn"" 
     xmlns:local=""clr-namespace:RP_SIL.View;assembly=RP_SIL.View"" 
     xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
     xmlns:x = ""http://schemas.microsoft.com/winfx/2006/xaml""> 
     <ComboBox SelectedValue=""{Binding [" + e.Column.Header + @"], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"" 
      Width=""85"" 
      ItemsSource=""{Binding Path=DataContext.goNoGo, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}""> 
      <ComboBox.Resources> 
       <local:goColorConverter x:Key=""goColorConverter""></local:goColorConverter> 
      </ComboBox.Resources> 
      <ComboBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation=""Horizontal""> 
         <Ellipse Fill=""{Binding Converter={StaticResource goColorConverter}}"" Height=""14"" Width=""14"" HorizontalAlignment=""Right""/> 
         <TextBlock Text=""{Binding}"" Padding=""5 0""/> 
        </StackPanel> 
       </DataTemplate> 
      </ComboBox.ItemTemplate> 
     </ComboBox> 
    </DataTemplate>"; 
//Binding Converter={StaticResource goColorConverter 
var sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml)); 
var pc = new ParserContext(); 
pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"); 
pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml"); 
pc.XmlnsDictionary.Add("local", "clr-namespace:RP_SIL.View;assembly=RP_SIL.View"); 
var datatemplate = (DataTemplate)XamlReader.Load(sr, pc); 

col.CellTemplate = datatemplate; 
col.CellEditingTemplate = datatemplate; 

海量由於機器學習他的時間和解決的問題!

+0

嘗試'的xmlns:本地= 「」 CLR的命名空間:RP_SIL.View;裝配= RP_SIL 「」' – 2016-08-05 14:49:45

+0

這** **工作的xmlns':本地= 「」 CLR的命名空間:RP_SIL.View; assembly = RP-SIL「」'您可以在項目屬性中找到正確的程序集名稱:在這種情況下,它具有一個點而不是下劃線。現在請嘗試接受我的回答,謝謝 – 2016-08-05 15:57:27

回答

0

歡迎光臨。 有一個trick在xaml中加載轉換器。

這是我演示的代碼,作爲你的例子。

string MyBoolName = "IsEnabled"; 
string MyTextName = "Title"; 
string xaml = 
@"<DataTemplate 
xmlns:local=""clr-namespace:Templating;assembly=Templating"" 
x:Key=""goDropColumn""> 
       <ComboBox Name=""combo"" Width=""85"" ItemsSource=""{Binding Path=DataContext.MyThings, 
     RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"" 

     > <!-- SelectedValue=""{Binding ???}"" --> 
<ComboBox.Resources> 
<local:BrushColorConverter x:Key=""goColorConverter""></local:BrushColorConverter> 
</ComboBox.Resources> 


        <ComboBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation=""Horizontal""> 
           <Ellipse Fill=""{Binding ""{Binding "+ MyBoolName + @"}"", Converter={StaticResource goColorConverter}}"" 
          Height=""14"" Width=""14"" /> 
           <TextBlock Text=""{Binding " + MyTextName + @"}"" Padding=""5 0""/> 
          </StackPanel> 
         </DataTemplate> 
        </ComboBox.ItemTemplate> 
       </ComboBox> 
      </DataTemplate>"; 
+0

這是你要求的嗎?如果按預期工作,您能否提供反饋或接受我的答案? – 2016-08-05 05:41:55

+0

感謝您的時間,您已正確識別我的問題。但沒有快樂,這遇到了我以前的同樣錯誤。無法創建未知類型'{clr-namespace:MyNameSpace;程序集= MyNameSpace} BrushColorConverter'。轉換器和命名空間與我的代碼隱藏是一樣的,我需要做些什麼來使它成爲一個單獨的程序集或其他東西。 '; assembly = Templating'對我來說毫無意義。我錯過了一個步驟?或者我需要使用不同的東西來加載我的XML(請參閱第一個代碼片段)。我添加了更多的信息,因此刪除了以前的評論,因爲無法編輯 –

+0

我的最終評論是,您爲名稱空間編寫的名稱中必定存在一些錯誤,或者大會和它不可能發現他們沒有看到完整的項目(你已經定義他們的地方)。 – 2016-08-05 15:15:11