本文介绍了使用WPF将ObservableCollection.Count绑定到标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的标签,它应该包括 .Count $ c> ObservableCollection 。



事情是,结果总是0(零)。



我在这里做错了什么?



这是我的代码:

 < Label ContentStringFormat =Members:{0} > 
< Label.Content>
< Binding Path =MembersList.CountMode =OneWayUpdateSourceTrigger =Default/>
< /Label.Content>
< / Label>

此属性如下所示:

  public static ObservableCollection< Mitglied> MembersList {get;组; } 


解决方案

>

MainWindow.Xaml.cs - >

  int Counter = 0; 
private static ObservableCollection< string> _MemberList = new ObservableCollection< string>();
//假设它是String类型。我把它作为String类型检查我的case
public static ObservableCollection< string> MemberList
{
get {return MainWindow._MemberList; }
set {MainWindow._MemberList = value; }
}

MainWindow()
{
InitializeComponent();
MemberList.Add(0);
MemberList.Add(1);
MemberList.Add(2);
Label1.DataContext = this;
}

private void Button_Click(object sender,RoutedEventArgs e)
{
try
{
MemberList.RemoveAt(Counter);
Counter ++;
}
catch(Exception ex)
{
string strTemp = ex.Message();
}
}

MainWindow.xaml - >

 < Grid> 
< Label Name =Label1ContentStringFormat =Members:{0}Margin =0,56,141,38RenderTransformOrigin =0.158,1.154Horizo​​ntalAlignment =RightWidth =183>
< Label.Content>
< Binding Path =MemberList.CountMode =OneWayUpdateSourceTrigger =Default/>
< /Label.Content>
< / Label>
< Button Click =Button_ClickWidth =100Height =20Content =clickMargin =43,169,360,122/>
< / Grid>


I have a simple Label that should include the bound .Count value of a Property of an ObservableCollection.

The thing is, that the result is always 0 (zero). The same Property is bound to a DataGrid, which works perfectly and even updates if something has changed in the Collection.

What am I doing wrong here?

Here is my code:

<Label ContentStringFormat="Members: {0}">
    <Label.Content>
        <Binding Path="MembersList.Count" Mode="OneWay" UpdateSourceTrigger="Default" />
    </Label.Content>
</Label>

The Property looks like:

public static ObservableCollection<Mitglied> MembersList { get; set; }
解决方案

You can try This...

MainWindow.Xaml.cs->

int Counter = 0;
private static ObservableCollection<string> _MemberList = new ObservableCollection<string>();
// Suppose it is of String type..I took it as of String type to check my case
public static ObservableCollection<string> MemberList
{
    get { return MainWindow._MemberList; }
    set { MainWindow._MemberList = value; }
}

MainWindow()
{
    InitializeComponent();
    MemberList.Add("0");
    MemberList.Add("1");
    MemberList.Add("2");
    Label1.DataContext = this;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    try
    {
        MemberList.RemoveAt(Counter);
        Counter++;
     }
     catch(Exception ex)
     {
         string strTemp=ex.Message();
     }
 }

MainWindow.xaml->

    <Grid>
        <Label Name="Label1" ContentStringFormat="Members: {0}" Margin="0,56,141,38" RenderTransformOrigin="0.158,1.154" HorizontalAlignment="Right" Width="183">
        <Label.Content>
            <Binding Path="MemberList.Count" Mode="OneWay" UpdateSourceTrigger="Default"/>
        </Label.Content>
    </Label>
        <Button Click="Button_Click" Width="100" Height="20" Content="click" Margin="43,169,360,122" />
    </Grid>

这篇关于使用WPF将ObservableCollection.Count绑定到标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:35