我有一个带有组合框的自定义控件。我想在下拉列表的末尾添加一个按钮,上面写着“AddNew”,例如“AddNewStudent”,单击该按钮时,会打开一个表格来注册新学生。
由于我希望创建自定义控件的许多实例,其中一些可能不需要此功能,因此我希望它是可激活的,反之亦然。例如,列出学生成绩的组合框不需要此功能,因为成绩是已知且不变的。此外,我希望单击“AddNew”按钮时采取的操作成为用户可设置的依赖属性,因为单击“AddNew”按钮时采取的操作对于每个实例都是不同的。
我没有看你的源代码。
我只会建议一种简单的典型方法来实现您需要的逻辑(当然,如果我正确理解您需要什么)。
public class SomeViewModel : ViewModelBase
{
public ObservableCollection<int> Ints { get; } = new() {0, 1, 2, 3, 4, 5 };
public RelayCommand AddInt => GetCommand(()=> Ints.Add(Ints.Count));
}
<Window ----------------------
----------------------
mc:Ignorable="d"
Title="AddItemWindow" Height="450" Width="800"
DataContext="{DynamicResource vm}">
<Window.Resources>
<local:SomeViewModel x:Key="vm"/>
<CompositeCollection x:Key="items">
<CollectionContainer Collection="{Binding Ints, Source={StaticResource vm}}"/>
<Button Content="Add" Command="{Binding AddInt, Source={StaticResource vm}}"/>
</CompositeCollection>
</Window.Resources>
<Grid>
<ComboBox ItemsSource="{Binding Mode=OneWay, Source={StaticResource items}}"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</Grid>
</Window>