2011-02-14 30 views
0

我目前在將XML綁定到Silverlight應用程序中的GUI時遇到問題。尤其是使用雙向綁定。在Silverlight中使用綁定到XML

正如我們所知,在使用WPF的Windows客戶端應用程序中執行操作確實很容易。 這裏你可以這樣做:

XML:

<person> 
    <firstname>Test</firstname> 
    <surname>Test</surname> 
    <email>[email protected]</email> 
</person> 

和查看網格編輯(使用XLINQ或XPath綁定)一個XAML頁面:

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="3*" /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="30" /> 
     <RowDefinition Height="30" /> 
     <RowDefinition Height="30" /> 
     <RowDefinition Height="221*" /> 
    </Grid.RowDefinitions> 
    <TextBlock Grid.Column="0" Grid.Row="0" Text="First name:" /> 
    <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Path=Element[firstname].Value, Mode=TwoWay}" /> 
    <TextBlock Grid.Column="0" Grid.Row="1" Text="Surname:" /> 
    <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=Element[surname].Value, Mode=TwoWay}" /> 
    <TextBlock Grid.Column="0" Grid.Row="2" Text="EMail:" /> 
    <TextBox Grid.Column="1" Grid.Row="2" Text="{Binding Path=Element[email].Value, Mode=TwoWay}" /> 
</Grid> 

由於中雙向模式用戶直接寫入XML。

但是,在Silverlight中沒有像上例那樣的綁定選項。但是微軟在Silverlight 4中添加了XPathEvaluate-Method()。

所以我試圖將完整的XDocument綁定到每個TextBox,並使用ConverterParameter傳遞一個XPath表達式並對其進行評估。

<Grid x:Name="LayoutRoot"> 
    <TextBlock Text="{Binding Path=Data, Converter={StaticResource TestKonverter}, ConverterParameter=//firstname, Mode=TwoWay}" FontSize="20" /> 
</Grid> 

和...

public class XMLConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     var __doc = (XDocument)value; 
     var __xpath = (IEnumerable)__doc.XPathEvaluate(parameter.ToString()); 

     return (__xpath.Cast<XElement>().FirstOrDefault()); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     //getting the attached XDocument again as __doc ?! 
     var __xpath = (IEnumerable)__doc.XPathEvaluate(parameter.ToString()); 

     (__xpath.Cast<XElement>().FirstOrDefault()).Value = value.ToString(); 

     return value; 
    } 
} 

爲了得到某種形式的雙向結合,我想到了使用XPath的表達,以獲得正確的節點,並寫在它的新價值。 問題是,在ConvertBack-Method()我看不到如何獲取XDocument的方法。有什麼辦法通過ConvertBack中的給定參數來獲取XDocument,而不是將其設置爲靜態的地方?

回答

0
從一個Silverlight論壇的問題

報價 -

「你可以創建一個類來保存您的數據和執行INotifyPropertyChanged,然後讀取或代碼編寫的XML

如果XML的格式不已經定義,你可以使用 '的DataContractSerializer'

編寫XML例子:

YourDataClassType data = new YourDataClassType(); 
    MemoryStream ms = new MemoryStream(); 
    DataContractSerializer ds = new DataContractSerializer(typeof(YourDataClassType)); 
    ds.WriteObject(ms, data); 
    ms.Flush(); 
    ms.Seek(0, SeekOrigin.Begin); 
    StreamReader sr = new StreamReader(ms); 
    string xml = sr.ReadToEnd(); 
    sr.Close(); 
    ms.Close(); 

讀取XML例子:

MemoryStream ms = new MemoryStream(); 
StreamWriter sw = new StreamWriter(ms); 
sw.Write(xml); 
sw.Flush(); 

ms.Seek(0, SeekOrigin.Begin); 

DataContractSerializer ds = new DataContractSerializer(typeof(YourDataClassType)); 
YourDataClassType data = ds.ReadObject(ms) as YourDataClassType; 
sw.Close(); 
ms.Close(); 

其中YourDataClassType是反映數據的類。這樣,您就不必自己解析XML「。

2

感謝您的回答。

大約兩個星期前,我解決了這個問題,僅通過創建一個名爲XTextBox新類。 我簡單的繼承文本框,並創建兩個DependencyProperties同類型的XElement和字符串中的新類。

在我的XAML我可以使用綁定和我一樣之前,交出完整的XElement和XPath來尋找合適的節點我的控制。

<custom:XTextBox Data="{Binding Path=XValues, Mode=TwoWay}" XPath="//Measurements/Headline/Fontsize" /> 

除了兩個DependencyProperties之外,我使用普通的TextChanged事件再次使用XPath更新XElement中的值。

這個完美解決了雙向綁定,因爲我從來不需要創建一個新的XElement並複製任何東西。我還實現了XComboBox,XRadioButton和XCheckBox,這對我來說已經足夠了。

相關問題