私の分離コードでは、クラスに次のものがあります。
public ObservableCollection<int> ints;
その値はコンストラクターで初期化されます。
ints = new ObservableCollection<int>();
次に、ラベルを次のようにバインドしていints
ます:
<Label Name="label" Content="{Binding Source={StaticResource ints}, Path=Count}"/>
プログラムを実行すると、次のことXamlParseException
が発生します。
「'System.Windows.StaticResourceExtension' に値を指定すると、例外がスローされました。」行番号「12」と行位置「20」。
バインディングラインに何か問題があると思います。助言がありますか?
この問題を説明する完全なデモ プログラムは次のとおりです。
XAML:
<Window x:Class="BindingObservableCollectionCountLabel.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">
<DockPanel>
<StackPanel>
<TextBox Name="textBox" Text="10"/>
<Button Name="add" Click="add_Click" Content="Add"/>
<Button Name="del" Click="del_Click" Content="Del"/>
<Label Name="label" Content="{Binding Source={StaticResource ints}, Path=Count}"/>
</StackPanel>
</DockPanel>
</Window>
C#:
using System;
using System.Windows;
using System.Collections.ObjectModel;
namespace BindingObservableCollectionCountLabel
{
public partial class MainWindow : Window
{
public ObservableCollection<int> ints;
public MainWindow()
{
InitializeComponent();
ints = new ObservableCollection<int>();
}
private void add_Click(object sender, RoutedEventArgs e)
{
ints.Add(Convert.ToInt32(textBox.Text));
}
private void del_Click(object sender, RoutedEventArgs e)
{
if (ints.Count > 0) ints.RemoveAt(0);
}
}
}