2013-05-02 100 views
0

我遇到了一個問題,我需要使用XML文件來管理我創建的插件中的各種類別。它由兩個部分組成:WPF中的雙向Treeview綁定

  1. 加載所有類別的啓動和使用的方法將它們添加到Outlook:

    AddCategory(字符串名稱,串色,串快捷方式)

  2. 能夠管理應用程序內的類別。

我已經得到儘可能AddCategory工作得很好,只要它是硬編碼。自從我開始使用XML以來,還沒有真正處理過它。我想如果我能弄清楚類別管理部分,這隻會落到實處。所以下面我已經發布了三段代碼,顯示的WPF使用HierarchicalDataTemplate嘗試顯示XML(僅列出郵箱)和XML文件本身。在我看來,我應該使用雙向綁定,以便我可以從管理界面添加和刪除部分XML文件。 從本質上講,我感到迷失於編寫此設置的最佳方式。我需要能夠輕鬆訪問各種類別,因爲它們很容易應用於郵箱。任何幫助或方向將不勝感激。我已經查看了與HDT,XML和WPF有關的所有其他各種問題,只是沒有進一步討論。我的LINQfu和我的綁定知識一樣薄弱。

WPF代碼

<Grid Name="mainGrid"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="30"/> 
     <RowDefinition Height="40"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="100"/> 
    </Grid.ColumnDefinitions> 
    <Grid.Resources> 
     <XmlDataProvider x:Key="CategoriesData" Source="Categories.xml" XPath="MailBoxes"/> 

     <!--Template for Rule--> 
     <HierarchicalDataTemplate x:Key="ruleHDT" 
            ItemsSource="{Binding [email protected]/Rule}"> 
      <TextBlock Text="{Binding [email protected]}" /> 
     </HierarchicalDataTemplate> 

     <!--Template for Category--> 
     <HierarchicalDataTemplate x:Key="categoryHDT" 
            ItemTemplate="{StaticResource ruleHDT}" 
            ItemsSource="{Binding [email protected]/Category}"> 
      <TextBlock Text="{Binding [email protected]}" /> 
     </HierarchicalDataTemplate> 

     <!--Template for MailBox--> 
     <HierarchicalDataTemplate x:Key="mailboxHDT" 
            ItemTemplate="{StaticResource categoryHDT}" 
            ItemsSource="{Binding [email protected]/MailBox}"> 
      <TextBlock Text="{Binding [email protected]}" /> 
     </HierarchicalDataTemplate> 

    </Grid.Resources> 
    <TreeView Grid.Column="0" Grid.Row="0" Margin="5" ItemsSource="{Binding Source={StaticResource CategoriesData}, XPath=MailBox}" 
       ItemTemplate="{StaticResource mailboxHDT}"/> 
    <StackPanel Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" Height="100" Margin="5" VerticalAlignment="Top"> 
     <Button Content="Add" Margin="5"/> 
     <Button Content="Remove" Margin="5" Width="80"/> 
    </StackPanel> 
    <StackPanel Grid.Column="0" Grid.Row="1" Margin="5" Orientation="Horizontal" VerticalAlignment="Center"> 
     <RadioButton Content="All MailBoxes" GroupName="MailBoxXMLView" Margin="0,0,5,0" /> 
     <RadioButton Content="Accessible MailBoxes" GroupName="MailBoxXMLView" Margin="5,0,0,0" IsChecked="True"/> 
    </StackPanel> 
    <StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" Orientation="Horizontal" Margin="5" VerticalAlignment="Center"> 
     <Button Content="Import" Margin="0,0,5,0" Width="80"/> 
     <Button Content="Export" Width="80"/> 
    </StackPanel> 
</Grid> 

XML文件

<?xml version="1.0" encoding="utf-8" ?> 
<MailBoxes> 
    <MailBox Name="MB01"> 
    <Categories> 
     <Category Name="Clean"> 
     <Color>olCategoryColorGreen</Color> 
     <Shortcut>olCategoryShortcutKeyCtrlF3</Shortcut> 
     <Rules> 
      <Rule Action="Only">Clean</Rule> 
     </Rules> 
     </Category> 
     <Category Name="Spam"> 
     <Color>olCategoryColorYellow</Color> 
     <Shortcut>olCategoryShortcutKeyCtrlF4</Shortcut> 
     <Rules> 
      <Rule Action="Remove">Clean</Rule> 
     </Rules> 
     </Category> 
    </Categories> 
    </MailBox> 
    <MailBox Name="MBTest01"> 
    <Categories> 
     <Category Name="Cat01"> 
     <Color>olCategoryColorRed</Color> 
     <Shortcut>olCategoryShortcutKeyNone</Shortcut> 
     <Rules> 
     </Rules> 
     </Category> 
     <Category Name="Cat02"> 
     <Color>olCategoryColorYellow</Color> 
     <Shortcut>olCategoryShortcutKeyNone</Shortcut> 
     <Rules> 
     </Rules> 
     </Category> 
    </Categories> 
    </MailBox> 
</MailBoxes> 

回答

1

XmlDataProvider不支持bidirectionnal綁定。 只有在需要公開某些數據時纔有用,但在您需要操作它們時並不真正有用。

如果你真的需要使用XML,而不是自定義的保存文件例如,在這裏誰解釋它是如何可能的鏈接手動保存在XML文件中的修改: http://www.codeproject.com/Articles/26875/WPF-XmlDataProvider-Two-Way-Data-Binding

+0

雖然這提供了一些有識之士來這個問題的雙向性,我仍然因爲能夠在TreeView中顯示所有數據而受到阻礙。目前只有第一級郵箱/郵箱部分工作。另外如果XmlDataProvider不是雙向的,你能提供一個更好的界面嗎?我曾經玩過XDocument,但現在已經到了。 – lordzero 2013-05-02 12:50:41