Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 绑定到数组元素_.net_Wpf_Xaml_Data Binding - Fatal编程技术网

.net 绑定到数组元素

.net 绑定到数组元素,.net,wpf,xaml,data-binding,.net,Wpf,Xaml,Data Binding,我试图将TextBlock绑定到ObservableCollection中的特定元素。 这就是我现在所做的: private ObservableCollection<double> arr = new ObservableCollection<double>(); public ObservableCollection<double> Arr { get { return arr; } set { arr = value; } } testBox.Dat

我试图将TextBlock绑定到ObservableCollection中的特定元素。 这就是我现在所做的:

private ObservableCollection<double> arr = new ObservableCollection<double>();
public ObservableCollection<double> Arr { get { return arr; } set { arr = value; }  }

testBox.DataContext = this;

private void Button_Click(object sender, RoutedEventArgs e)
{
   Arr[0] += 1.0;
}

    [ValueConversion(typeof(ObservableCollection<double>), typeof(String))]
    public class myObsCollConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ObservableCollection<double> l = value as ObservableCollection<double>;
            if( l == null )
                return DependencyProperty.UnsetValue;
            int i = int.Parse(parameter.ToString());

            return l[i].ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    }


    <Window.Resources>
        <local:myObsCollConverter x:Key="myConverter"/>
    </Window.Resources>

        <TextBlock Name="testBox" Text="{Binding Path=Arr,Converter={StaticResource myConverter}, ConverterParameter=0}" />
private observetecollection arr=new observetecollection();
公共可观测集合Arr{get{return Arr;}set{Arr=value;}
testBox.DataContext=this;
私有无效按钮\u单击(对象发送者,路由目标e)
{
Arr[0]+=1.0;
}
[ValueConversion(typeof(ObservableCollection)、typeof(String))]
公共类MyobCollConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
ObservableCollection l=作为ObservableCollection的值;
if(l==null)
返回dependencProperty.unset值;
int i=int.Parse(parameter.ToString());
返回l[i].ToString();
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回dependencProperty.unset值;
}
}
我看到的是testBox在创建时显示了Arr的第一个值。 但它并没有反映出这个元素的任何变化。
要在我的文本框中查看对Arr[0]的更改,我必须做些什么?

ObservableCollection
s不会将更改传播到属于集合的对象中存储的值。它仅在集合本身的内容发生更改(即添加、删除、重新排序的项)时触发通知。如果您想在集合中的值发生变化时更新UI,则必须单独连接该集合。

无需转换器。您可以像这样直接绑定到
Arr[0]

 <TextBlock Name="testBox" Text="{Binding Path=Arr[0]}"/>
然后

 ObservableCollection<MyDouble> Arr { get; set; }
observeCollection Arr{get;set;}
绑定到

 <TextBlock Name="testBox" Text="{Binding Path=Arr[0].Value}"/>

在我的例子中,您可以使用这种方式,我想从布尔数组绑定可见性: 代码隐藏:

using System.Windows;
public static readonly DependencyProperty ButtonVisibleProperty =
        DependencyProperty.Register("ButtonVisible", typeof(BindingList<Boolean>), typeof(BaseWindow), new PropertyMetadata(new BindingList<Boolean>()));
 public BindingList<Boolean> ButtonVisible 
    { 
        get { return (BindingList<Boolean>)GetValue(BaseWindow.ButtonVisibleProperty); }
        set 
        {
            SetValue(BaseWindow.ButtonVisibleProperty, value);
            OnPropertyChanged("ButtonVisible");  
        } 
    }

当心!在我的例子中,祖先的ancestorType依赖于一个窗口

这是替换项,而不是修改对象中的内部数据。我刚刚检查过,在这种情况下它将触发CollectionChanged事件(带有替换操作)。但是它是否应该工作呢?当值被替换时,ObservableCollection将引发CollectionChanged事件。。。我很惊讶绑定没有注意到这一点。好吧,您绑定到元素而不是集合,所以我猜UI将只侦听PropertyChanged事件如果我不想将元素索引(0)绑定到另一个控件怎么办?e、 g.我有一个组合框来选择元素的索引,我的测试框应该显示该索引。基于该解决方案(即使用Observable collection并直接绑定到Arr[0]),我必须在Arr.CollectionChanged上定义一个事件回调,在该回调中我调用了OnPropertyChanged(“Arr”)。这样,当元素发生更改时,绑定可以工作。
using System.Windows;
public static readonly DependencyProperty ButtonVisibleProperty =
        DependencyProperty.Register("ButtonVisible", typeof(BindingList<Boolean>), typeof(BaseWindow), new PropertyMetadata(new BindingList<Boolean>()));
 public BindingList<Boolean> ButtonVisible 
    { 
        get { return (BindingList<Boolean>)GetValue(BaseWindow.ButtonVisibleProperty); }
        set 
        {
            SetValue(BaseWindow.ButtonVisibleProperty, value);
            OnPropertyChanged("ButtonVisible");  
        } 
    }
Visibility=""{Binding Path=ButtonVisible[0], RelativeSource={RelativeSource AncestorType={x:Type Window}}}"