简体   繁体   English

WPF中的双向Treeview绑定

[英]Bidirectional Treeview Binding in WPF

I've come across a problem in which I need to use an XML file to manage various categories in an addin I'm creating. 我遇到了一个问题,我需要使用XML文件来管理要创建的插件中的各种类别。 It consists of two parts: 它由两部分组成:

  1. Load all categories in the start and use a method to add them into Outlook: 首先加载所有类别,然后使用一种方法将其添加到Outlook中:

    AddCategory(string name, string color, string shortcut) AddCategory(字符串名称,字符串颜色,字符串快捷方式)

  2. Be able to manage the categories from within the application. 能够从应用程序内部管理类别。

I've gotten as far as the AddCategory works pretty well as long as it's hardcoded. 只要AddCategory可以硬编码,我就可以很好地工作。 Have not really dealt with it much since I've moved to using XML. 自从我开始使用XML以来,还没有真正处理过。 I figured if I could get the category management section figured out, this would just fall into place. 我认为,如果我能弄清楚类别管理部分,那就可以了。 So below I've posted three sections of code, the WPF for the display that is using HierarchicalDataTemplate to attempt to display the XML (only gets as far as listing the MailBoxes) and the XML file itself. 因此,在下面,我发布了三部分代码,即使用HierarchicalDataTemplate尝试显示XML(仅列出邮箱的信息)的WPF和XML文件本身。 It would seem to me I should be using a bidirectional binding so that I can add and remove parts of the XML file from the management interface. 在我看来,我应该使用双向绑定,以便可以从管理界面添加和删除XML文件的某些部分。 In essence I feel lost in what the best way is to code this setup. 本质上,我对最好的方法是编写此设置感到迷惑。 I need to be able to easily access the various categories as they apply to a mailbox pretty easily. 我需要能够轻松访问各种类别,因为它们很容易应用于邮箱。 Any help or direction would be greatly appreciated. 任何帮助或指示将不胜感激。 I've looked at all the other various questions that have to do with HDT, XML and WPF on here and just haven't gotten any further. 在这里,我已经研究了与HDT,XML和WPF有关的所有其他各种问题,并且没有进一步讨论。 My LINQfu is weak as is my knowledge of binding. 我的LINQfu和我的绑定知识都很薄弱。

WPF Code 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 XPath=@Rules/Rule}">
            <TextBlock Text="{Binding XPath=@Action}" />
        </HierarchicalDataTemplate>

        <!--Template for Category-->
        <HierarchicalDataTemplate x:Key="categoryHDT"
                                  ItemTemplate="{StaticResource ruleHDT}"
                                  ItemsSource="{Binding XPath=@Categories/Category}">
            <TextBlock Text="{Binding XPath=@Name}" />
        </HierarchicalDataTemplate>

        <!--Template for MailBox-->
        <HierarchicalDataTemplate x:Key="mailboxHDT"
                                  ItemTemplate="{StaticResource categoryHDT}"
                                  ItemsSource="{Binding XPath=@MailBoxes/MailBox}">
            <TextBlock Text="{Binding XPath=@Name}" />
        </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 File 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>

XmlDataProvider doesn't support bidirectionnal Binding. XmlDataProvider不支持双向绑定。 It's only useful when you want expose some data but not really when you need to manipulate them. 它仅在您要公开一些数据时才有用,而在需要操纵它们时才真正有用。

If you really need to use xml and not a custom save file for example, here a link who explain how it's possible to manually save the modification in the xml file: http://www.codeproject.com/Articles/26875/WPF-XmlDataProvider-Two-Way-Data-Binding 例如,如果您确实需要使用xml而不是自定义保存文件,请在此处提供一个链接,该链接解释了如何在xml文件中手动保存修改: http : //www.codeproject.com/Articles/26875/WPF- XmlDataProvider双向数据绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM