我想允许用户选择一个 xml 文件(通过单击菜单项),以便尽可能使用代码隐藏中的 xml 数据提供程序对其进行处理。我该怎么做?
我不能像这样绑定源代码: <XmlDataProvider x:Key="ProductsXml" Source="{Binding OpenRecordMenuItem_Click}"/>
我知道我可以通过菜单项上的单击处理程序等来处理 xml。
这是一点点示例,希望对您有所帮助。您可以简单地使用菜单项单击事件更新按钮代码的单击事件。
主窗口。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="228,80,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Label Content="Label" Name="lblXmlFileName" HorizontalAlignment="Left" Margin="57,78,0,0" VerticalAlignment="Top" Width="144"/>
<StackPanel x:Name="stackpanel1">
<StackPanel.Resources>
<XmlDataProvider x:Name="provider" x:Key="provider1">
</XmlDataProvider>
</StackPanel.Resources>
</StackPanel>
</Grid>
代码隐藏:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// update validations as per your requirements...
var dialog = new OpenFileDialog();
dialog.Filter = "XML Files|*.xml";
if (dialog.ShowDialog() == true)
{
lblXmlFileName.Content = dialog.FileName.ToString();
var provider1 = (XmlDataProvider) stackpanel1.Resources["provider1"];
if(provider1 != null)
provider1.Source = new Uri(dialog.FileName, UriKind.Absolute);
}
}
}