简体   繁体   English

WPF列表框绑定XElement未更新

[英]wpf listbox binding XElement not updated

I am a beginner in C# / xaml and I do not understand everything in the binding, among others if it is necessary to notify or not to refresh the view. 我是C#/ xaml的初学者,我对绑定中的所有内容都不了解,尤其是在有必要通知或不刷新视图的情况下。

In my code I use XML and XElement to store data. 在我的代码中,我使用XML和XElement来存储数据。 The xml contains one list of items (Properties), each of these items contains two lists (videoStreams and audioStreams). xml包含一个项目列表(属性),每个项目包含两个列表(videoStreams和audioStreams)。

When I add (or delete) one video the view is not refreshed. 当我添加(或删除)一部视频时,视图不会刷新。 After if I add (or delete) one audio the view is refershed. 如果我添加(或删除)一个音频后,则视图将被引用。 The videos and audios lists are updated. 视频和音频列表已更新。 If, in the xaml code, I reverse audio and video lists, the problem is with audio. 如果在xaml代码中反转音频和视频列表,则问题出在音频方面。

Have you any idea about this problem? 您对这个问题有任何想法吗?

My XML (extract) 我的XML(摘录)

<Root>
<Properties name="Node">
    <videoNode exclude="false">
        <rate minFrameRate="" maxFrameRate="" />
    </videoNode>
    <audioNode >
        <bitrateRange min="" max="" />
    </audioNode>
</Properties>

The xaml XAML

    <ListBox Name="multiMaster" ItemsSource ="{Binding Elements[Properties]}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <ListBox Name="videoStreams" ItemsSource="{Binding Elements[videoNode]}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel x:Name="stackVideo" Orientation="Vertical">
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <ListBox Name="audioStreams" ItemsSource="{Binding Elements[audioNode]}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel x:Name="stackAudio" Orientation="Vertical">
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

and the add function 和添加功能

    foreach (XElement childElement in oMMPNode.Descendants("Properties"))
{
    if (childElement.Attribute("id").Value == profileId)
    {
        XElement newVideo = new XElement("videoNode");
        childElement.Add(newVideo);

        break;
    }
}

Yes, it is necessary to notify. 是的,有必要通知。 You need to map the XML data to a set of ViewModels . 您需要将XML数据映射到一组ViewModels These should implement INotifyPropertyChanged , and raise the corresponding event when values are changed, etc. 这些应实现INotifyPropertyChanged ,并在更改值时引发相应事件,等等。

So, instead of XElement you would use: 因此,您可以使用XElement代替XElement:

public class XElementViewModel:INotifyPropertyChanged
{ 

 public XElementViewModel(XElement data)
 {
    // initialize properties 
 }
 ... 
 // here you define all needed properties of XElement that you want to expose
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM