Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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
C# ItemsControl不更新ItemsSource绑定_C#_Wpf_Xaml - Fatal编程技术网

C# ItemsControl不更新ItemsSource绑定

C# ItemsControl不更新ItemsSource绑定,c#,wpf,xaml,C#,Wpf,Xaml,我将observedcollection绑定到ItemsControl作为ItemsSource,从VM到视图的绑定工作正常,但如果我更改文本框中的绑定内容,它将不会更新绑定到的observedcollection 我似乎不明白为什么,有人知道这是为什么吗 这是我的密码: <ItemsControl ItemsSource="{Binding Metrics, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Grid.Column="1

我将
observedcollection
绑定到
ItemsControl
作为
ItemsSource
,从VM到视图的绑定工作正常,但如果我更改
文本框中的绑定内容,它将不会更新绑定到的
observedcollection

我似乎不明白为什么,有人知道这是为什么吗

这是我的密码:

<ItemsControl ItemsSource="{Binding Metrics, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Grid.Column="1" Grid.Row="1" Margin="0, 20, 0, 0">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <WrapPanel Orientation="Horizontal">
                <TextBox Name="CalibrationNameTB"  Grid.Column="1" Text="{Binding ., UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Style="{StaticResource baseStyle}" Margin="0, 1" Padding="5, 1" Width="270" FontSize="12"/>
            </WrapPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl" >
            <StackPanel Orientation="Horizontal" >
                <ItemsPresenter />
            </StackPanel>
        </ControlTemplate>
    </ItemsControl.Template>
</ItemsControl>

无法更新
字符串,因为它是不可变的

您应该做的是将
observateCollection
替换为
observateCollection
,其中
YourType
是一个具有公共
string
属性的类,您可以获取或设置该属性:

class YourType : INotifyPropertyChanged
{
    private string _theString;
    public string TheString
    {
        get { return _theString; }
        set { _theString = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
然后在XAML标记中绑定到此属性:

<WrapPanel Orientation="Horizontal">
    <TextBox Name="CalibrationNameTB"  Grid.Column="1" Text="{Binding TheString, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource baseStyle}" Margin="0, 1" Padding="5, 1" Width="270" FontSize="12"/>
</WrapPanel>


Text=“{Binding.}”
不会替换ItemsSource集合中的字符串。您必须使用带有字符串属性的包装器类。@克莱门斯,您能为我指出如何实现这一点的正确方向吗?不要使用带有字符串属性的MyItem类的
ObservableCollection
,也要发布ViewModel代码请