我正在WPF應用程序中,我需要動態生成2個組框,其中包含單選按鈕,文本框和組合框。那麼我遇到了一個棘手的情況,兩個groupbox中的值需要不同。未能在WPF中動態生成的UI組件中設置不同的值
我有兩個xaml文件PCM2PDMView.xaml
和PCM2PDMWidgetView.xaml
& viewmodel類。
PCM2PDMView.xaml:
<UserControl.Resources>
<DataTemplate x:Key="PCM2PDMDataTemplate">
<WrapPanel>
<TextBlock Text="{Binding Description}" Margin="5,5,0,0"/>
<local:PCM2PDMWidgetView Margin="5,10,5,5"/>
</WrapPanel>
</DataTemplate>
</UserControl.Resources>
<Grid Style="{DynamicResource styleBackground}" >
<ItemsControl ItemTemplate="{StaticResource PCM2PDMDataTemplate}" ItemsSource="{Binding PCM2PDMWidgets}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
PCM2PDMWidgetView.xaml:
<Grid>
<GroupBox Height="Auto" HorizontalAlignment="Stretch" Margin="5" Name="groupBox1" VerticalAlignment="Stretch" Width="Auto">
<Grid>
<Grid Grid.Row="1">
<RadioButton Grid.Column="1" Content="Port B" Command="{Binding PortCommand}" IsChecked="{Binding PortCheck}" Height="20" Width="60" HorizontalAlignment="Center" Margin="0,0,0,0" Name="PCM2PDMEnableRadioBtn" VerticalAlignment="Center" />
</Grid>
<Grid Grid.Row="2">
<ComboBox Grid.Column="1" Height="20" ItemsSource="{Binding PortModeList}" SelectedItem="{Binding SelectedPortModeList, Mode=OneWayToSource}" SelectedIndex="0" HorizontalAlignment="Center" Margin="0,0,0,0" Name="PortModeCombo" VerticalAlignment="Center" Width="110" />
</Grid>
<Grid Grid.Row="3">
<ToggleButton Grid.Column="0" Content="Soft Reset" Height="25" Width="105" HorizontalAlignment="Center" Margin="0,0,0,0" Name="SoftResetRadioBtn" VerticalAlignment="Center" />
<ToggleButton Grid.Column="1" Command="{Binding PCM2PDMEnableCommand}" IsChecked="{Binding PCM2PDMCheck}" Content="PCM2PDM Enable" Height="25" Width="110" HorizontalAlignment="Center" Margin="0,0,0,0" Name="PCM2PDMRadioBtn" VerticalAlignment="Center" />
</Grid>
<Grid Grid.Row="4">
<TextBox Height="25" IsReadOnly="True" Text="{Binding MemAddPoint}" Margin="0" Name="SetRead" />
</Grid>
</Grid>
</GroupBox>
</Grid>
PCM2PDmViewModel.cs:
public ObservableCollection<PCM2PDMWidgetViewModel> PCM2PDMWidgets { get; set; }
public PCM2PDMViewModel()
{
PCM2PDMWidgets = new ObservableCollection<PCM2PDMWidgetViewModel>();
PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel { Description = "PCM2PDM 0", ID = 0 });
PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel { Description = "PCM2PDM 1", ID = 1 });
}
這會在內部生成與關聯的UI控件相關的2個組框。
PCM2PDMWidgetViewModel.cs:
public ObservableCollection<string> _PortModeList = _PortModeList = new ObservableCollection<string>();
_PortModeList.Add("Normal"); //Adding in Constructor
_PortModeList.Add("Mode 1 - PCM + PDM");
_PortModeList.Add("Mode 2 - PDM only");
public ObservableCollection<string> PortModeList
{
get { return _PortModeList; }
set
{
_PortModeList = value;
OnPropertyChanged("PortModeList");
}
}
private string _selectedPortModeList;
public string SelectedPortModeList
{
get { return _selectedPortModeList; }
set
{
_selectedPortModeList = value;
OnPropertyChanged("SelectedPortModeList");
}
}
public string Description {get; set;}
public int ID {get; set;}
public string MemAddPoint {get; set;}
要求:
現在,如果你注意到
Grid.Row=1
,你會發現一個名爲單選按鈕Port B
,那麼我的要求是有Port B
在我第一個groupbox和Port C
在我的第二個groupbox中。功能(buttonclick方法)對於兩者都是通用的,但名稱必須不同。我們如何實現這一目標?在我的組合框中,我添加了3個項目。在這裏,我想在第一組框中的組合框中添加
Normal
和Mode 1 - PCM + PDM
,並在第二組框中的組合框中添加Normal
,Mode 1 - PCM + PDM
和Mode 2 - PDM Only
。這怎麼能實現? :)如果你發現
Grid.Row=3
你會發現PCM2PDMEnable
togglebutton。我希望在我的第二個分組框中顯示此切換按鈕,而不是在我的第一個分組框中。我怎樣才能做到這一點?Grid.Row=4
中的文本框有一個值爲(例如0x5)的文本框。我需要將此文本框的文本設置爲Groupbox 1中的0x5和Groupbox2的文本框中的0x7。怎麼做?
請幫助:)
解決方案查詢2但其投擲錯誤。我做了以下事情:'公共PCM2PDMWidgetViewModel(列表comboBoxItems) {foreach(在comboBoxItems中的字符串項目){_PortModeList.Add(item); PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel(new List {「Normal」,「Mode 1 - PCM + PDM」}){Description =「PCM2PDM 0」,ID = 0,PortName =「Port B」}); PCM2PDMWidgets.Add(new PCM2PDMWidgetViewModel(new List {「Normal」,「Mode 1-PCM + PDM」,「Mode 2」}){Description =「PCM2PDM 1」,ID = 1,PortName =「Port C」}) ;' –
StonedJesus
PCM2PDMWidgetViewModel(List comboBoxItems)..應該是PCM2PDMWidgetViewModel(列表 comboBoxItems)..請發佈你正在得到的錯誤 –
Bathineni
,它的作品就像一個魅力:)謝謝兄弟:) – StonedJesus