【问题标题】:Retrieve and save the Dynamic added control in WPF Xaml?在 WPF Xaml 中检索并保存动态添加的控件?
【发布时间】:2017-03-24 04:55:29
【问题描述】:

I have added the template based on this link

我有一个 添加 按钮 - 当我通过命令单击它时,我将它添加到集合中。

 <ItemsControl ItemsSource="{Binding Collection}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid DataContext="{StaticResource VieWModel}">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="15*"/>
                                <ColumnDefinition Width="40*"/>
                            </Grid.ColumnDefinitions>
                            <Label Content="GH" Grid.Row="0" Grid.Column="0" VerticalContentAlignment="Center"></Label>
                            <tk:RadComboBox Grid.Row="0" Grid.Column="0" Margin="10" IsFilteringEnabled="True" Width="150" DisplayMemberPath="D"  IsEditable="True" ItemsSource="{Binding GK}"  SelectedItem="{Binding SK, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="SelectionChanged">
                                        <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </tk:RadComboBox>
                            <Label Content="HB" Grid.Row="0" Grid.Column="1" VerticalContentAlignment="Center"></Label>
                            <tk:RadComboBox  Grid.Row="0" Grid.Column="1" Margin="10" IsFilteringEnabled="True" Name="cb"  Width="350" IsEditable="True" DisplayMemberPath="D"  ItemsSource="{Binding VR}" SelectedItem="{Binding VR1,Mode=TwoWay}">
                            </tk:RadComboBox>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

ViewModel 示例代码:

     // Property for selected Item in combox1
            Public ValBase  SK{get;set;}

        //Property off combobox1 binding
        Public ValBase GK{get;set;}

        // Property ofor selected Item in combox2
        Public ValBase VR1{get; set;}

        //Property ofr combobox2 binding
        Public ValBase VR{get;set;}

        Public void AddButton(object obj)
        {
          var item =new collectionbase();
          Collection.Add(item)
        }

每当我单击添加按钮时,都会添加此 itemplate。

我的需求

  1. 当我第一次点击添加按钮时,应该会添加模板
  2. 当我第二次点击添加按钮 以前生成的控件 必须包含值,然后才应将控件添加到集合中,然后应创建新控件
  3. 而且我不知道如何保存在集合中动态创建的那些值

我的想法已经用完了,任何人都可以提供帮助。 MVVM 模式

【问题讨论】:

  • 你不应该在给定 ItemsSource 时为 DataTemplate 设置 DataContext,根据 MVVM 更改它并查看我的答案

标签: c# wpf xaml


【解决方案1】:

我猜你在 MainViewModel 中有 Collection 和用于添加模型的命令。

private Model _lastAdded;
public Model LastAdded
{
  get{return _lastAdded;}
  set{_lastAdded = value;}
}

private void AddCommand(object obj)
{
  if(_lastAdded != null && _lastAdded.SelectedValue != null)
  {
    var newItem = new Model();
    Collection.Add(newItem);
    _lastAdded = newItem;
   }
   else
   {
     //Show message
   }

}

【讨论】:

  • 在哪里添加 LastAdded 你能解释一下,我希望它不会被第一次添加
  • @SundarStalin 您拥有 Collection 和 AddCommand 方法的同一类。
  • 我已经将它添加到 MyCollection。当我添加下一次时,我的组合框必须只有值才应该保存。
  • 查看我的编辑。您应该使用 _lastAdded.SelectedValue 中 ComboBox 选择中使用的属性。如果可以,请发布模型视图模型代码
  • 我已经更新了 DemoViewModel 代码,请您使用正确的变量名称更新完整的解决方案,就像在 viewmodel 中一样
【解决方案2】:

使用选择器类。我已经使用 CurrentInstance 来绑定集合,它现在可以正常工作了

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    相关资源
    最近更新 更多