C# WPF中的字典失败绑定

C# WPF中的字典失败绑定,c#,wpf,dictionary,binding,C#,Wpf,Dictionary,Binding,我的应用程序中的大多数文本块和单选按钮对象都绑定到我的字典 我将新值存储在字典中 问题是绑定失败引发了一个非常缓慢的异常 下面是一个示例:(此代码运行正常,如果在start应用程序中找不到匹配项,则运行速度很慢。) 如果字典为空,是否有方法使连接不引发异常解决此问题的一种方法是覆盖字典 此外,在主窗口中,在调用InitializeComponent()后,必须初始化措辞,如下所示: 这将把DependencyObjects的实例和您要绑定到的相应DependencyProperty添加到字典中。

我的应用程序中的大多数文本块和单选按钮对象都绑定到我的字典

我将新值存储在字典中

问题是绑定失败引发了一个非常缓慢的异常

下面是一个示例:(此代码运行正常,如果在start应用程序中找不到匹配项,则运行速度很慢。)


如果字典为空,是否有方法使连接不引发异常

解决此问题的一种方法是覆盖
字典

此外,在主窗口中,在调用InitializeComponent()后,必须初始化措辞,如下所示:


这将把DependencyObjects的实例和您要绑定到的相应DependencyProperty添加到字典中。

我使用您的类CustomDictionary

这是我的xaml文件

<StackPanel>
    <TextBox Name="textBox1" Text="{Binding Dictio[0], Mode=TwoWay}"/>
    <TextBox Name="textBox2" Text="{Binding Dictio[1], Mode=TwoWay}"/>
    <RadioButton Name="radioButton" GroupName="select" IsChecked="{Binding Dictio[2], Mode=TwoWay}" Content="true"/>
<Button Content="test" Click="test"/>
</StackPanel>
在你们班里习惯用语

 return properties[index].Item1.GetValue(properties[index].Item2);

您还可以使用空值初始化字典,然后将绑定模式设置为OneWayToSource,如下所示:

public MainWindow()
{
    Dictio = new Dictionary<int, object>();
    for (int i = 0; i < 200; i++)
    {
        Dictio.Add(i, new object());
    }
    InitializeComponent();
    this.DataContext = this;
}

<StackPanel>
    <TextBox Text="{Binding Dictio[0], Mode=OneWayToSource}"/>
    <TextBox Text="{Binding Dictio[1], Mode=OneWayToSource}"/>
</StackPanel>
public主窗口()
{
dictiono=新字典();
对于(int i=0;i<200;i++)
{
添加(i,新对象());
}
初始化组件();
this.DataContext=this;
}

您可以使用转换器进行绑定,该转换器将在返回值之前检查字典的大小。您是否可以解释更多关于转换器的信息?如果您有示例,我该如何开发?谢谢您的回复,但当我想显示字典的内容时,我有一个函数private void test的错误示例(objectsender,RoutedEventArgs e){Console.Out.WriteLine(Dictio[1]);}我有一个错误“l'exception NullReferenceException n'a pasétégérée”在您尝试显示其元素之前,您是否初始化了Dictio?如何初始化Dictio?我想通过上述xaml文件填充Dictio,如上图所示。然后您可以创建转换器或将其绑定到代码中。我已更改了答案,请尝试一下,并告诉我您忘记了将元素添加到Dictio中,就像I d一样答案中的id。你也不应该在xaml中绑定。我想在xaml中绑定,因为我有150个控件,所以我不能在后面的代码中添加所有控件,我想在xaml中绑定它们,这就是为什么你能给出控件外观的示例?(xaml)与我创建的包含文本框和单选按钮的示例xaml文件相同。但我无法从构造函数初始化控件。这是一个测验,因此我必须将响应存储在“Dictio”中以后要保存在数据库中的词典我不明白为什么不能从构造函数初始化词典。MainWindow.xaml是什么样子的?是的,我终于成功绑定了,没有错误,谢谢@Miro Bucko
public class CustomDictionary : IEnumerable
{
    private Dictionary<int, Tuple<DependencyObject, DependencyProperty>> properties = new Dictionary<int, Tuple<DependencyObject, DependencyProperty>>();

    IEnumerator IEnumerable.GetEnumerator()
    {
        foreach (var item in properties)
        {
            yield return  new KeyValuePair<int, object>(item.Key, item.Value.Item1.GetValue(item.Value.Item2));
        }
    }

    public void Add(int index, DependencyObject obj, DependencyProperty property)
    {
        properties.Add(index, new Tuple<DependencyObject, DependencyProperty>(obj, property));
    }

    public object this[int index]
    {
        get
        {
            return properties[index].Item1.GetValue(properties[index].Item2);
        }
        set
        {
            properties[index].Item1.SetCurrentValue(properties[index].Item2, value);
        }
    }
}
private CustomDictionary _Dictio;

public CustomDictionary Dictio
{
    get { return _Dictio; }
    set { _Dictio = value; }
}
public MainWindow()
{
    InitializeComponent();

    Dictio = new CustomDictionary();
    Dictio.Add(0, textBox1, TextBox.TextProperty);
    Dictio.Add(1, textBox2, TextBox.TextProperty);
    Dictio.Add(2, radioButton, RadioButton.IsCheckedProperty );

    this.DataContext = this;
}
<StackPanel>
    <TextBox Name="textBox1" Text="{Binding Dictio[0], Mode=TwoWay}"/>
    <TextBox Name="textBox2" Text="{Binding Dictio[1], Mode=TwoWay}"/>
    <RadioButton Name="radioButton" GroupName="select" IsChecked="{Binding Dictio[2], Mode=TwoWay}" Content="true"/>
<Button Content="test" Click="test"/>
</StackPanel>
    private CustomDictionary _Dictio;

 public MainWindow()
        {
            Dictio = new CustomDictionary();
            this.DataContext = this;
            InitializeComponent();
        }

    public CustomDictionary Dictio
            {
                get { return _Dictio; }
                set { _Dictio = value; }
            }

    private void test(object sender, RoutedEventArgs e)
    {
        foreach (KeyValuePair<int, object> kvp in Dictio)
                {
                    Console.Out.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
            }
    }
Une exception de première chance de type 'System.Collections.Generic.KeyNotFoundException' s'est produite dans mscorlib.dll
 return properties[index].Item1.GetValue(properties[index].Item2);
public MainWindow()
{
    Dictio = new Dictionary<int, object>();
    for (int i = 0; i < 200; i++)
    {
        Dictio.Add(i, new object());
    }
    InitializeComponent();
    this.DataContext = this;
}

<StackPanel>
    <TextBox Text="{Binding Dictio[0], Mode=OneWayToSource}"/>
    <TextBox Text="{Binding Dictio[1], Mode=OneWayToSource}"/>
</StackPanel>