2010-07-20 48 views
2

我有一個按鈕,它從文件加載XAML,從中創建一個控件,並將此控件作爲子項添加到作爲模板的一部分的畫布,該畫布是存在於dockpanel資源中的模板的一部分在窗口上。該窗口還有一個名爲cboTColour的組合框和一個名爲cboBColour的組合框,我用它來爲我的加載控件設置一個簡單的漸變背景。加載的XAML不能正確地綁定到現有元素

我加載XAML和使用下面的代碼添加到我的畫布:

XmlReader xaml = XmlReader.Create(filename); 
newControl = (Viewbox)XamlReader.Load(xaml); 
((Canvas)(testButton.Template.FindName("MyCanvas", testButton))).Children.Clear(); 
((Canvas)(testButton.Template.FindName("MyCanvas", testButton))).Children.Add(newControl); 

這裏是XAML我加載:

<?xml version="1.0" encoding="utf-8"?> 
<Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Document" Stretch="Fill"> 
    <Canvas Height="64" Width="128" ClipToBounds="True"> 
     <Canvas.Background> 
      <!--Horizontal Gradient--> 
      <LinearGradientBrush StartPoint="1,0"> 
       <GradientStop Color="{Binding ElementName=cboTColour, Path=SelectedItem.Name}" Offset="0"></GradientStop> 
       <GradientStop Color="{Binding ElementName=cboBColour, Path=SelectedItem.Name}" Offset="1"></GradientStop> 
      </LinearGradientBrush> 
     </Canvas.Background> 
    </Canvas> 
</Viewbox> 

我試圖把XAML直入設計師和它完美的作品,所以這不是一個問題。當我從文件加載XAML時,控件正在創建並正確放置,但數據綁定不起作用 - 顏色不會改變。我收到以下錯誤:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=cboTColour'. BindingExpression:Path=SelectedItem.Name; DataItem=null; target element is 'GradientStop' (HashCode=24393646); target property is 'Color' (type 'Color') 
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=cboBColour'. BindingExpression:Path=SelectedItem.Name; DataItem=null; target element is 'GradientStop' (HashCode=23972246); target property is 'Color' (type 'Color') 

我以爲發生了什麼事是當XAMLReader加載XAML廣告創建一個從它的控制,這是不知道的路徑,我的組合框的,因爲XAML是尚未窗口的一部分,並且當控件添加到窗口時,該綁定不會更新,但我不知道如何修改XAML中的綁定以反映我的組合框相對於它的位置或者修改XAMLReader或整體數據環境以考慮我的控件。我還可以向您保證,當代碼在窗口上按下按鈕時,組合框將按照這一點創建組合框。

我必須指定我不能修改代碼中的綁定本身,因爲綁定將出現在我將加載的不同XAML文件的各個位置和不同時間。

任何幫助將不勝感激。

回答

1

它看起來像XamlReader對它加載的xaml進行限制,所以它不能包含在加載的xml中未定義的未知元素名稱。相反,使用數據綁定到性能很好,但需要一些代碼隱藏。當用戶更改組合框的選定項目時,下面的代碼將更改加載的控件的背景。您需要添加一個值轉換器,將顏色轉換爲供組合框使用的可讀名稱。

XAML:

<StackPanel> 
    <ComboBox 
     ItemsSource="{Binding Path=AvailableColors}" 
     SelectedValuePath="Color" 
     SelectedValue="{Binding Path=SelectedColors[color0].Color}" /> 
    <ComboBox 
     ItemsSource="{Binding Path=AvailableColors}" 
     SelectedValuePath="Color" 
     SelectedValue="{Binding Path=SelectedColors[color1].Color}" /> 
    <ContentControl Name="newControl" /> 
</StackPanel> 

後面代碼:

using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Windows; 
using System.Windows.Markup; 
using System.Windows.Media; 
using System.Xml; 

namespace LoadTest 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 

      SelectedColors = new Dictionary<string, ColorInfo>(); 
      SelectedColors.Add("color0", AvailableColors.First()); 
      SelectedColors.Add("color1", AvailableColors.Last()); 

      XmlReader xaml = XmlReader.Create("newcontrol.xml"); 
      newControl.Content = XamlReader.Load(xaml); 
      newControl.DataContext = SelectedColors; 

      DataContext = this; 
     } 

     public List<ColorInfo> AvailableColors 
     { 
      get 
      { 
       return new List<ColorInfo>() 
       { 
        new ColorInfo() {Color = Colors.Red }, 
        new ColorInfo() { Color = Colors.Green }, 
        new ColorInfo() { Color = Colors.Blue }, 
        new ColorInfo() { Color = Colors.Yellow } 
       }; 
      } 
     } 

     public Dictionary<string, ColorInfo> SelectedColors { get; private set;} 
    } 

    public class ColorInfo : INotifyPropertyChanged 
    { 
     private Color _color; 
     public Color Color 
     { 
      get { return _color; } 
      set 
      { 
       _color = value; 
       if (PropertyChanged != null) 
       { 
        PropertyChanged(this, new PropertyChangedEventArgs("Color")); 
       } 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
    } 
} 

XAML文件加載(newcontrol.xml):

<?xml version="1.0" encoding="utf-8"?> 
<Viewbox 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Name="Document" Stretch="Fill"> 
    <Canvas Height="64" Width="128" ClipToBounds="True"> 
    <Canvas.Background> 
     <LinearGradientBrush StartPoint="1,0"> 
     <GradientStop Color="{Binding Path=[color0].Color}" Offset="0" /> 
     <GradientStop Color="{Binding Path=[color1].Color}" Offset="1" /> 
     </LinearGradientBrush> 
    </Canvas.Background> 
    </Canvas> 
</Viewbox> 
+0

工作出色,謝謝。 – 2010-07-21 10:15:59