有没有办法将方法绑定到ListBox的DataTemplate?

时间:2009-11-22 11:17:34

标签: c# wpf data-binding xaml listbox

以下示例在ListBox中成功显示了属性标题,但有没有办法显示方法 GetTitle(),这样我就不必全部转我的方法进入属性?

e.g。这些似乎都不起作用:

<TextBlock Text="{Binding GetTitle}"/>
<TextBlock Text="{Binding GetTitle()}"/>

XAML:

<Window x:Class="TestBindMethod8938.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <ListBox DockPanel.Dock="Top"  ItemsSource="{Binding BackupTasks}" Margin="0 10 0 0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Title}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

代码隐藏:

using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace TestBindMethod8938
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {

        #region ViewModelProperty: BackupTasks
        private ObservableCollection<BackupTask> _backupTasks = new ObservableCollection<BackupTask>();
        public ObservableCollection<BackupTask> BackupTasks
        {
            get
            {
                return _backupTasks;
            }

            set
            {
                _backupTasks = value;
                OnPropertyChanged("BackupTasks");
            }
        }
        #endregion


        public Window1()
        {

            InitializeComponent();
            DataContext = this;

            BackupTasks.Add(new BackupTask(@"c:\test", @"c:\test2"));

        }


        #region INotifiedProperty Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }



    public class BackupTask
    {
        public string SourceFolder { get; set; }
        public string TargetFolder { get; set; }
        public int NumberOfFilesToBackup { get; set; }
        public string Title
        {
            get
            {
               return SourceFolder + " --> " + TargetFolder + " (" + NumberOfFilesToBackup + ")";
            }
            set
            {
            }
        }

        public BackupTask(string sourceFolder, string targetFolder)
        {
            SourceFolder = sourceFolder;
            TargetFolder = targetFolder;
        }

        public string GetTitle()
        {
            return SourceFolder + " --> " + TargetFolder + " (" + NumberOfFilesToBackup + ")";
        }


    }

}

2 个答案:

答案 0 :(得分:2)

可能通过ObjectDataProvider。见http://bea.stollnitz.com/blog/?p=22

<ObjectDataProvider ObjectInstance="{StaticResource odp1}" MethodName="WeightOnPlanet" x:Key="odp2">
    <ObjectDataProvider.MethodParameters>
        <system:Double>95</system:Double>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

或者自己编写Markup Extension。见http://joyfulwpf.blogspot.com/2008/12/creating-custom-wpf-markup-extension-to.html

答案 1 :(得分:2)

使用列表框的'DisplayMember'属性不是更容易吗? 我建议使用Properties而不是Methods,因此数据绑定和NotifyPropertyChanged按预期工作。我不认为绑定到方法实际上会起作用......

像这样:

<ListBox DockPanel.Dock="Top" ItemsSource="{Binding BackupTasks}" 
         DisplayMemberPath="Title" Margin="0 10 0 0">

希望这有帮助, [R

相关问题