I am working on a WPF application where I need to dynamically generate 2 group boxes which have a radio button, textbox and a combobox inside. Well I have come across a tricky situation where values needs to be different in both groupboxes.
I have two xaml files PCM2PDMView.xaml and PCM2PDMWidgetView.xaml & the viewmodel classes.
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 });
}
This generates 2 groupboxes with associated UI controls inside.
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;}
Requirement:
Now if you notice
Grid.Row=1you will find a radiobutton titledPort B, Well my requirement is to havePort Bin my 1st groupbox andPort Cin my second groupbox. The functionality(buttonclick method) will be common for both but the name must be different. How can we achieve that?In my combobox I am adding 3 items. Here I want to add
NormalandMode 1 - PCM + PDMin combobox present in 1st groupbox and addNormal,Mode 1 - PCM + PDMandMode 2 - PDM Onlyin combobox present in 2nd groupbox. How can that be achieved? :)If you notice
Grid.Row=3you will findPCM2PDMEnabletogglebutton. I want to have this togglebutton visible in my 2nd groupbox and not in my first groupbox. How can I do that?Textbox in
Grid.Row=4has a textbox whose value is (e.g. 0x5). I need to set the text of this textbox as 0x5 in Groupbox 1 and 0x7 in Textbox of Groupbox2. How to do that?
Please Help :)