简体   繁体   English

在选定的indexchange事件触发后填充控件

[英]Populate control after selectedindexchange event has fired

I have a MVVM WPF project where I have an devexpress accordian control which is populated with xml template items from a ViewModel. 我有一个MVVM WPF项目,其中有一个devexpress Accordian控件,其中装有ViewModel的xml模板项。 That works great, but my problem is when I click on one of the items in the accordian control and the selectedIndexChanged event is fired. 那很好,但是我的问题是当我在Accordian控件中单击其中一项时,就会触发selectedIndexChanged事件。 I want to handle that in the MVVM manner and get the selected items value(which is a path to an xml file) from the accordian control, fetch the content of the xml file and databind a textbox control with the content of the xml file. 我想以MVVM方式处理该问题,并从Accordian控件中获取选定的项目值(这是xml文件的路径),获取xml文件的内容,并将文本框控件与xml文件的内容进行数据绑定。 The following is what I have tried so far. 以下是到目前为止我尝试过的。

Here is my xaml user control 这是我的XAML用户控件

<dxa:AccordionControl Grid.Column="0" x:Name="accordianTemplateMenu" 
SelectionMode="Single" SelectionUnit="SubItemOrRootItem" ItemsSource="
{Binding TemplateItems}"
  ChildrenPath="TemplateItems" DisplayMemberPath="Header >
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EventToCommand EventName="SelectedItemChanged" Command="
{Binding EditCommand}">
            <dxmvvm:EventToCommand.EventArgsConverter>
                <Common:AccordionEventArgsConverter/>
            </dxmvvm:EventToCommand.EventArgsConverter>
        </dxmvvm:EventToCommand>
    </dxmvvm:Interaction.Behaviors>
</dxa:AccordionControl>

<GridSplitter Grid.Column="1" />

<TextBlock Grid.Column="2" x:Name="templateItemContainer">
    <Run Name="run" Text="{Binding XML}" ></Run>
</TextBlock>

This boils down to the AccordionEventArgsConverter which gets me the event arguments from the selecteditem in the accordian control: 归结为AccordionEventArgsConverter ,它从Accordian控件中的selecteditem中获取事件参数:

public class AccordionEventArgsConverter : 
EventArgsConverterBase<AccordionSelectedItemChangedEventArgs>
{
    protected override object Convert(object sender, 
AccordionSelectedItemChangedEventArgs args)
    {
        if (args != null)
        {
            return args;
        }
        return null;
    }
}

And finally my viewmodel: 最后是我的视图模型:

class TemplateMenuViewModel
{
    private List<TemplateItem> _templateItems;

    public TemplateMenuViewModel()
    {
        EditCommand = new DelegateCommand<object>(Edit, CanEdit);
    }

    public List<TemplateItem> TemplateItems
    {
        get
        {
            TemplateProvider provider = new TemplateProvider();
            return provider.GetTemplateMenuItems("pathToMenuItems");
        }
        set { _templateItems = value; }
    }

    public ICommand<object> EditCommand { get; private set; }

    public void Edit(object accordianItemArgs)
    {

    }

    public bool CanEdit(object accordianItemArgs)
    {
        return accordianItemArgs != null;
    }
}

I am able to get into the public void Edit method, which is great because from there I can use the accordianItemArgs to get the xml content, but how do I "return"/databind the xml content to the textblock element in the xaml file? 我可以进入public void Edit方法,这很棒,因为从那里可以使用accordianItemArgs获取xml内容,但是如何将xml内容“返回” /数据绑定到xaml文件中的textblock元素?

There are a couple of things: 有两件事:

  • You need the TemplateMenuViewModel to define an XML property. 您需要TemplateMenuViewModel来定义XML属性。 It looks like your TextBlock is already binding to it. 看来您的TextBlock已经绑定到它了。
  • Then you need your ViewModel to implement the INotifyPropertyChanged interface. 然后,您需要使用ViewModel来实现INotifyPropertyChanged接口。 It doesn't look like you're doing that, then raise a property changed event when the XML text is set. 看起来好像不是那样,然后在设置XML文本时引发一个属性更改事件。
  • You should set your Text="{Binding XML}" with a Mode of OneWay: 您应该使用OneWay模式设置Text="{Binding XML}"

    Text="{Binding XML, Mode=OneWay}" Text =“ {Binding XML,Mode = OneWay}”

If you need more information on how to implement INotifyPropertyChanged , check out this tutorial: https://www.tutorialspoint.com/mvvm/mvvm_first_application.htm . 如果您需要有关如何实现INotifyPropertyChanged更多信息,请查看本教程: https : //www.tutorialspoint.com/mvvm/mvvm_first_application.htm

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

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