动态堆栈的ComboBoxes试图添加新项目

时间:2022-08-31 23:12:06

I have an Items control bound to a observable collection of TaskActivity objects.

我有一个Items控件绑定到一个可观察的TaskActivity对象集合。

<ItemsControl ItemsSource="{Binding TasksActivities, UpdateSourceTrigger=PropertyChanged}" Margin="20, 0, 20, 20">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Margin="2, 0, 2, 0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width= "*" />
                    <ColumnDefinition Width= "*" />
                    <ColumnDefinition Width= "*" />
                    <ColumnDefinition Width= "70"/>
                </Grid.ColumnDefinitions>

                <ComboBox x:Name="test" IsEditable="True" ItemsSource="{Binding Source={StaticResource Locator}, Path=Main.AvailableActivities, Mode=TwoWay}" SelectedValue="{Binding ActivityId}" Text="{Binding Name, UpdateSourceTrigger=LostFocus}" SelectedValuePath="Key" DisplayMemberPath="Value" HorizontalAlignment="Stretch" Grid.Column="0">
                </ComboBox>
                <TextBox Text="{Binding Length}" Grid.Column="1" />
                <TextBox Text="{Binding Comment}" Grid.Column="2" />

                <Button Height="24" Content="Remove" HorizontalAlignment="Right" Margin="10, 0, 10, 0" Style="{StaticResource LinkButton}" Grid.Column="3">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <cmd:EventToCommand Command="{Binding Source={StaticResource Locator}, Path=Main.DeleteActivityCommand, Mode=OneWay}" CommandParameter="{Binding Name}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

When a value is typed in the combobox that is doesn't exist and focus is lost, I want to have a prompt appear asking if they want to add add that value to the AvailableActivities list (which is just a Dictionary). Right now the border just goes red, and while it does update the "Name" property of the object inside the OC, it's not real because it can't set the ActivityId since it doesn't actually exist in the list of AvailableActivities.

当组合框中输入的值不存在并且焦点丢失时,我想出现一个提示,询问是否要将该值添加到AvailableActivities列表(这只是一个字典)。现在边界变成红色,虽然它确实更新了OC内对象的“Name”属性,但它并不真实,因为它无法设置ActivityId,因为它实际上并不存在于AvailableActivities列表中。

I've tried an EventToCommand for SelectionChanged and LostFocus, but when a new value is entered, the value I'm returned is 'null' so I can't add it.

我已经为SelectionChanged和LostFocus尝试了一个EventToCommand,但是当输入一个新值时,我返回的值为'null',所以我无法添加它。

Normally I could just bind the text value to a property on the VM and just do it all there, but since it's a property inside a ObservableCollection of TaskActivity objects I'm not sure that's possible.

通常我可以将文本值绑定到VM上的属性,然后在那里完成所有操作,但由于它是TaskActivity对象的ObservableCollection中的属性,我不确定是否可行。

Any suggestions for achieving this functionality?

有关实现此功能的任何建议吗?

1 个解决方案

#1


1  

First make the ComboBox.Text a two-way binding so that the view model property gets updated:

首先使ComboBox.Text成为双向绑定,以便更新视图模型属性:

Text="{Binding Path=Name, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"

With that in place, add a command to the ComboBox's LostFocus event:

有了这个,添加一个命令到ComboBox的LostFocus事件:

<ComboBox>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="LostFocus">
            <cmd:EventToCommand Command="{Binding Source={StaticResource Locator}, 
                                                  Path=Main.AddNewActivityCommand}" 
                                CommandParameter="{Binding ElementName=test,Path=Text}"
            />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

And finally, the "AddNewActivityCommand" should just add the new item (in the "Name" property) to the "AvailableActivities" collection.

最后,“AddNewActivityCommand”应该只将新项目(在“Name”属性中)添加到“AvailableActivities”集合中。

#1


1  

First make the ComboBox.Text a two-way binding so that the view model property gets updated:

首先使ComboBox.Text成为双向绑定,以便更新视图模型属性:

Text="{Binding Path=Name, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"

With that in place, add a command to the ComboBox's LostFocus event:

有了这个,添加一个命令到ComboBox的LostFocus事件:

<ComboBox>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="LostFocus">
            <cmd:EventToCommand Command="{Binding Source={StaticResource Locator}, 
                                                  Path=Main.AddNewActivityCommand}" 
                                CommandParameter="{Binding ElementName=test,Path=Text}"
            />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

And finally, the "AddNewActivityCommand" should just add the new item (in the "Name" property) to the "AvailableActivities" collection.

最后,“AddNewActivityCommand”应该只将新项目(在“Name”属性中)添加到“AvailableActivities”集合中。