After searching and trying several options over the last week, I can't seem to find what I am looking for; maybe someone here can help. While reading through this, please keep in mind that I am attempting to utilize MVVM as strictly as possible, though I am relatively new to WPF. As a side note, I am using Mahapps.Metro to style my window and controls, found here.
I have an XML file that my application uses for configuration (I cannot use the app.config file because the application cannot install on the users' systems). The application will look for this file at start-up and if it does not find the file, it will create it. Below is a snippet of the XML:
<?xml version="1.0" encoding="utf-8"?>
<prefRoot>
<tabReport>
<cbCritical>True</cbCritical>
</tabReport>
</prefRoot>
I reference the XML file in my Window.Resources
:
<Controls:MetroWindow.Resources>
<XmlDataProvider x:Key="XmlConfig"
Source="%appdata%\Vulnerator\Vulnerator_Config.xml"
XPath="prefRoot"
IsAsynchronous="False"
IsInitialLoadEnabled="True"/>
</Controls:MetroWindow.Resources>
And utilize this as the DataContext
for my MainWindow
:
<Controls:MetroWindow DataContext="{DynamicResource XmlConfig}">
Next, I set up a "string-to-bool" converter:
class StringToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
bool? isChecked = (bool?)value;
return isChecked;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
string isChecked = value.ToString();
return isChecked;
}
return string.Empty;
}
}
Finally, I bind IsChecked
to the appropriate XPath
:
<Checkbox x:Name="cbCritical"
Content="Critical"
IsChecked="{Binding XPath=//tabReport/cbCritical,
Converter={StaticResource StringToBool}}" />
After all of this, the applciation loads, but IsChecked
is set to false
... Any and all ideas would be helpful here; thanks in advance!