none
如何绑定List<T>.count? RRS feed

  • 问题

  • 如何绑定List<T>.count。

    把一个按钮的IsEnable绑定到数据集List<T>的记录数上。

    在记录数改变的时候,不起作用,初始化的时候起作用了,难道与INotifyPropertyChanged接口有关系?

    绑定

    bMod.SetBinding(Button.IsEnabledProperty, new Binding("Count")
                {
                    Source = myList,
                    Mode = BindingMode.OneWay,
                    Converter = new IntToBooleanConverter()
                });

    转换器

    class IntToBooleanConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                bool isEnable = false;
                switch ((int)value)
                {
                    case 0:
                        isEnable = false;
                        break;
                    default:
                        isEnable = true;
                        break;
                }
                return isEnable;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    2013年9月29日 7:32

答案

  • 您好,

    >>在记录数改变的时候,不起作用,初始化的时候起作用了,难道与INotifyPropertyChanged接口有关系?

    第一次设置绑定时,绑定是成功的,按钮会被禁止,之后对myList添加Item后,是不会通知Button的绑定更新数据的,需要实现 INotifyPropertyChanged 接口。

    对于您的代码,可以在对myList更新后,重新设置一次绑定,即可更新Button的状态:

    public MainWindow()
    {
                InitializeComponent();
    
                setBinding();
    }
    
    private void setBinding()
    {
                bMod.SetBinding(Button.IsEnabledProperty, new Binding("Count")
                {
                    Source = myList,
                    Mode = BindingMode.OneWay,
                    Converter = new IntToBooleanConverter()
                });
    
                tb1.SetBinding(TextBlock.TextProperty, new Binding("Count") { Source = myList });
    }
    
    private void Button_Click(object sender, RoutedEventArgs e)
    {
                myList.Add(new Customer() { ID = "1", Name = "Test" });
    
                setBinding();
    }

    XAML:

    <Grid>
            <TextBlock Name="tb1" FontWeight="Bold" FontSize="20" />
            <StackPanel VerticalAlignment="Center" Orientation="Horizontal">
                <Button Name="bMod" Margin="50" Width="100" Height="50" Content="TestButton" />
                <Button Content="Click" Margin="50" Width="100" Height="50" Click="Button_Click" />
            </StackPanel>
        </Grid>

    请阅读以下参考资料:

    #How to: Create a Binding in Code
    http://msdn.microsoft.com/en-us/library/ms742863.aspx


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • 已标记为答案 FoolRabbit 2013年10月2日 2:32
    2013年9月30日 9:17
    版主