日期:2014-05-18  浏览次数:20997 次

wpf listview不同列数据绑定
我想在listview中显示3列数据Data1, Data2, Sum
其中Sum=Data1+Data2,我想用绑定的方式实现,我是这样做的:
声明元数据类型:
C# code

// Data类型,包含Data1和Data2属性,用来绑定
public class MyData : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private decimal data1;
        private decimal data2;
        public decimal Data1
        {
            get { return data1; }
            set
            {
                data1 = value;
                if (PropertyChanged != null)
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Data1"));
            }
        }
        public decimal Data2
        {
            get { return data2; }
            set
            {
                data2 = value;
                if (PropertyChanged != null)
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Data2"));
            }
        }
    }
// MyDatas数据类型,用于标示集合
    public class MyDatas : ObservableCollection<MyData>
    { }

// XAML中代码
// 设置资源
    <Window.Resources>
        <local:MyDatas x:Key="MyDatas"/>   
    </Window.Resources>
// 显示数据
    <StackPanel>
        <Button Click="Button_Click">add people</Button>
        <Button Click="Button_Click_1">change comany</Button>
        <ListView ItemsSource="{StaticResource MyDatas}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Data1" DisplayMemberBinding="{Binding Data1}"/>
                    <GridViewColumn Header="Data2" DisplayMemberBinding="{Binding Data2}"/>
                    <GridViewColumn Header="Sum" DisplayMemberBinding="{Binding ???}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </StackPanel>


对于Data1和Data2的Binding已经实现,但是如何让Sum列自动绑定到Data1+Data2?也就是代码中'???'部分应该如何填写?
如果用Converter,那么假如我只binding了Data1 再用一个Converter那么Data2变化的时候明显不会触发Converter,Sum也不会变,请问大家应该怎么解决呢?
真心求教,请大家指点,非常感谢!

------解决方案--------------------
用MultiBinding组合多个Binding