开发者

Binding ListBox ItemsSource to method call results at compile time?

开发者 https://www.devze.com 2023-01-29 14:53 出处:网络
I\'m new to WPF data binding. I have a ListBox on a form that I want to bind to the results of the following method call:

I'm new to WPF data binding.

I have a ListBox on a form that I want to bind to the results of the following method call:

RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32)
    .OpenSubKey(@"SOFTWARE\Vendor\Product\Systems").GetSubKeyNames();

At the moment I'm doing it at runtime by assigning ListBox.ItemsSource = (method); in the Window_Loaded() event handler. But this means that the source data for the control is non-obvious when looking at the control configuration in the form editor.

Is there a way to configure this binding within the XAML so that it is visible in the form editor, to make the behavior of the code easier to understand?

Most of the examples in the MSDN documentation bind the controls to static resources, like in-line XAML resources. I've noticed that there is an ObjectDataProvider class which provides "[...] the ability to bind to the result of a method." However I am finding the examples in the ObjectDataProvider documentation quite confusing. I'd appreciate some advice on whether that's the right way to do thi开发者_开发技巧s binding, and if so, what syntax to use when declaring the ObjectDataProvider.


In short, I don't think you can use such a complex statement directly in your XAML. As you've found, it is possible to bind to the result of calling a method of an object via ObjectDataProvider, but your expression is a chain of method calls that I believe cannot be used to source ObjectDataProvider directly in XAML.

You should instead think about implementing a separated presentation pattern such as Model-View-ViewModel to expose the result of your expression via a collection property on a ViewModel that you then bind as the DataContext of your view (Window).

Something like:

MainWindow.xaml

<Window x:Class="WpfApplication10.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>
        <ItemsControl ItemsSource="{Binding Items}"/>
    </Grid>
</Window>

MainWindow.cs

using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Win32;

namespace WpfApplication10 {
    public class ViewModel {
        public IEnumerable<String> Items {
            get { return RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Vendor\Product\Systems").GetSubKeyNames(); }
        }
    }

    /// <summary>
    ///   Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
            DataContext = new ViewModel();
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消