Reputation: 59
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. It consists of two parts:
Load all categories in the start and use a method to add them into Outlook:
AddCategory(string name, string color, string shortcut)
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. Have not really dealt with it much since I've moved to using 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. 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. 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. My LINQfu is weak as is my knowledge of binding.
WPF Code
<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 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>
Upvotes: 0
Views: 250
Reputation: 620
XmlDataProvider doesn't support bidirectionnal Binding. 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
Upvotes: 1