我正在我的wpf应用程序中处理单选按钮和组合框。虽然我是一名C++开发人员,但我最近转到了C#。我的应用程序处理上述组件的动态生成。基本上我已经在我的应用程序中创建了4个动态单选按钮,在单击每个按钮时,我应该将不同的项目添加到我的组合框中。代码如下:
XAML:
<ItemsControl ItemsSource="{Binding Children}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<RadioButton Content="{Binding RadioBase}" Margin="0,10,0,0" IsChecked="{Binding BaseCheck}" GroupName="SlotGroup" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ComboBox Visibility="{Binding IsRegisterItemsVisible}" ItemsSource="{Binding RegComboList}" SelectedItem="{Binding SelectedRegComboList, Mode=TwoWay}" SelectedIndex="0" />FPGARadioWidgetViewModel类:
public ObservableCollection<FPGAViewModel> Children { get; set; }
public FPGARadioWidgetViewModel()
{
Children = new ObservableCollection<FPGAViewModel>();
Children.Add(new FPGAViewModel() { RadioBase = "Base 0x0", ID = 0 });
Children.Add(new FPGAViewModel() { RadioBase = "Base 0x40", ID = 1 });
Children.Add(new FPGAViewModel() { RadioBase = "Base 0x80", ID = 2 });
Children.Add(new FPGAViewModel() { RadioBase = "Base 0xc0", ID = 3 });
}FPGAViewModel类:
private bool sBaseCheck;
public bool BaseCheck
{
get { return this.sBaseCheck; }
set
{
this.sBaseCheck = value;
AddComboItems();
this.OnPropertyChanged("BaseCheck");
}
}
private ObservableCollection<string> _RegComboList;
public ObservableCollection<string> RegComboList
{
get { return _RegComboList; }
set
{
_RegComboList = value;
OnPropertyChanged("RegComboList");
}
}
private void AddComboItems()
{
int baseRegister = 0x40 * ID;
ObservableCollection<string> combo = new ObservableCollection<string>();
for (int i = 0; i < 0x40; i++)
{
int reg = (i * 8) + baseRegister;
combo[i] = "0x" + reg.ToString("X");
}
RegComboList = new ObservableCollection<String>(combo);
OnPropertyChanged("RegComboList");
}
private bool isRegisterItemsVisible = false;
public bool IsRegisterItemsVisible
{
get { return isRegisterItemsVisible; }
set
{
isRegisterItemsVisible = value;
OnPropertyChanged("IsRegisterItemsVisible");
OnPropertyChanged("RegComboList");
}
}如果您注意到,在单击某个单选按钮时,它应该根据ID在combobox中添加具有不同值的项。必须确保在单击任何单选按钮时,只应添加该单选按钮中的项,而应清除combobox的先前内容。我正试着用上面的代码做同样的事情,但是当我调试的时候似乎没有任何东西出现在combobox中。
请帮助:)
发布于 2012-11-03 19:09:18
这是因为combobox已绑定到不同的字符串引用。每次单选单击都会更改项目,并返回一个列表的新引用。尝试重用当前RegComboList,方法是删除其中的现有项,然后添加新项。如果无法更新组合框,则双向绑定到RegComboList。
编辑:这是一个.Net 4.5的工作示例,不用担心概念是一样的。
这里的组合框最初加载了5个单词。然后,每次用户单击该按钮时,都会将一组新的单词放入列表中,从而更改组合框。请注意为集合完成的清除。

<Window x:Class="WPFCombo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50*" />
<RowDefinition Height="50*" />
<RowDefinition Height="172*" />
</Grid.RowDefinitions>
<Button Content="Button"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="75"
Click="Button_Click_1" />
<ComboBox HorizontalAlignment="Center"
Grid.Row="1"
ItemsSource="{Binding RegComboList}"
VerticalAlignment="Center"
Width="120" />
</Grid>
</Window>代码隐藏
public partial class MainWindow : Window, INotifyPropertyChanged
{
private const string cLorem = @"Lorem ipsum dolor sit amet consectetur adipiscing elit Maecenas
et lacinia nisl Aenean nec aliquet risus Phasellus dui purus
sollicitudin nec cursus vitae cursus id purus Nam quis risus
velit Sed aliquam tellus in odio pulvinar tincidunt Sed bibendum mi";
private int Skip { get; set; }
private ObservableCollection<string> _RegComboList;
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<string> RegComboList
{
get { return _RegComboList; }
set
{
_RegComboList = value;
OnPropertyChanged();
}
}
public MainWindow()
{
InitializeComponent();
RegComboList = new ObservableCollection<string>();
GenerateWords(5);
DataContext = this;
}
private void GenerateWords(int toTake)
{
RegComboList.Clear();
Regex.Split(cLorem, @"\s+").Skip(Skip)
.Take(toTake)
.ToList()
.ForEach(word => RegComboList.Add( word ));
Skip += toTake;
}
protected virtual void OnPropertyChanged( [CallerMemberName] string propertyName = "" )
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler( this, new PropertyChangedEventArgs( propertyName ) );
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
GenerateWords(new Random().Next(1, 10));
}
}https://stackoverflow.com/questions/13208461
复制相似问题