Silverlight / WPF - Accessing the selected index in the listbox in Silverlight MVVM by having xaml as items

Asked By Ramachandran on 12-Jun-12 01:03 AM
Jitendra Faye replied to Ramachandran on 12-Jun-12 01:06 AM
Follow These links-

http://forums.silverlight.net/t/223925.aspx/1

http://www.dotnetspider.com/forum/292639-How-get-selected-item-listbox-click-button.aspx


here you will get example code for this,.
Ramachandran replied to Jitendra Faye on 12-Jun-12 01:13 AM

<Grid Grid.Row="1">


<StackPanel Style="{StaticResource CustomStackPanel}">


<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Height="300" Width="950">


<ScrollViewer.Content>


<ContentControl x:Name="groupofcontrols" IsEnabled="{Binding contentenableordisable}">


<Grid x:Name="groupofcontrolsgrid" Background="{Binding Path=Color}">


<ListBox Grid.Row="1" Name="FactorsListBox" ItemsSource="{Binding FactorsList,Mode=TwoWay}">


<ListBox.ItemTemplate>


<DataTemplate>


<StackPanel>


<Grid>


<Grid.RowDefinitions>


<RowDefinition Height="2*"/>


</Grid.RowDefinitions>


<Grid Grid.Row="0">


<Grid.ColumnDefinitions>


<ColumnDefinition Width="150"/>


<ColumnDefinition Width="140"/>


<ColumnDefinition Width="90"/>


<ColumnDefinition Width="100"/>


<ColumnDefinition Width="90"/>


<ColumnDefinition Width="30"/>


<ColumnDefinition Width="60"/>


<ColumnDefinition Width="120"/>


<ColumnDefinition Width="90"/>


</Grid.ColumnDefinitions>


<Grid.RowDefinitions>


<RowDefinition Height="40"/>


<RowDefinition Height="40"/>


<RowDefinition Height="40"/>


<RowDefinition Height="40"/>


<RowDefinition Height="40"/>


</Grid.RowDefinitions>


<TextBox Grid.Row="0" Margin="0,0,0,0" Text="{Binding Name,Mode=TwoWay}" IsEnabled="True" Grid.Column="0" Height="27" Width="100" HorizontalAlignment="Left" Name="txtName"/>


<TextBox Grid.Row="1" Grid.Column="0" Width="150" Height="70" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" Name="txtdesc" Text="{Binding Description, Mode=TwoWay}" Grid.RowSpan="2" AcceptsReturn="True"/>


<TextBlock FontWeight="Bold" Grid.Row="0" Grid.Column="1" Height="27" Margin="0,0,0,0" Width="130" Text="Impacts or Revenue" Name="txtblkImpactsorRevenue"/>


<RadioButton Name="rdbtnImpacts" GroupName="RB1" Grid.Row="1" Margin="10,0,0,0" IsChecked="{Binding Path=Impact,Mode=TwoWay}" VerticalAlignment="Bottom" Grid.Column="1" Content="Impacts" ></RadioButton>


<RadioButton Name="rdbtnRevenue" GroupName="RB1" Grid.Row="2" Margin="10,0,0,0" IsChecked="{Binding Path=Revenue,Mode=TwoWay,ElementName=FactorsListBox}" VerticalAlignment="Top" Grid.Column="1" Content="Revenue" ></RadioButton>


<TextBlock FontWeight="Bold" Text="Months Applied To" Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="2" Height="27" HorizontalAlignment="Center" Width="190" Name="txtblkMonthsAppliedTo"/>


<TextBlock Name="txtblkFrom" Grid.Row="1" Grid.Column="2" Width="90" Text="From" Height="27"/>


<TextBlock Name="txtblkTo" Grid.Row="2" Grid.Column="2" Width="90" Text="To" Height="27"/>


<ComboBox Name="ddlfrommonth" Width="90" Grid.Row="1" Grid.Column="3" Height="27"

ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ScrollViewer}, Path=DataContext.FromMonthList}"

DisplayMemberPath="MonthName"

SelectedValuePath="MonthName"

SelectedValue="{Binding FromMonth,Mode=TwoWay}"/>


<ComboBox Name="ddlTomonth" Width="90" Grid.Row="2" Grid.Column="3" Height="27"

ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ScrollViewer}, Path=DataContext.ToMonthList}"

DisplayMemberPath="MonthName"

SelectedValuePath="MonthName"

SelectedValue="{Binding ToMonth,Mode=TwoWay}" />


<TextBlock FontWeight="Bold" Text="Max%Min%" Grid.Row="0" Grid.Column="4" Height="27" Width="90" Name="txtblkMaxMinPercent"/>


<TextBox Name="MaxPercen" KeyDown="MaxPercen_KeyDown" MaxLength="3" Grid.Row="1" Grid.Column="4" Width="90" Height="27" DataContext="{StaticResource VM}" Text="{Binding MaxperValue,Mode=TwoWay}"/>


<TextBox Name="MinPercen" MaxLength="2" Grid.Row="2" KeyDown="MinPercen_KeyDown" Grid.Column="4" Width="90" Height="27" DataContext="{StaticResource VM}" Text="{Binding MinperValue,Mode=TwoWay}"/>


<Slider Name="WireFrameSlider" Orientation="Vertical" Minimum="{Binding MinperValue,Mode=TwoWay}" Maximum="{Binding MaxperValue,Mode=TwoWay}" DataContext="{StaticResource VM}" Value="{Binding zoomslider,Mode=TwoWay}" Height="120" Grid.Row="0" Grid.Column="5" Grid.RowSpan="3" Width="30" Cursor="Arrow"></Slider>


<TextBlock FontWeight="Bold" Text="Effect" Grid.Row="0" Grid.Column="6" Height="27" Width="90" Name="txtblkEffect"/>


<TextBlock Text="{Binding ElementName=WireFrameSlider,Path=Value,Mode=TwoWay}" Grid.Row="1" Grid.Column="6" Height="27" Width="90" Name="txtblkEffectPercentValue"/>


<TextBlock FontWeight="Bold" Text="Enabled/Disabled" Grid.Row="0" Grid.Column="7" Height="27" Width="120" Name="txtblkEnabledorDisabled"/>


<RadioButton Name="rdbtnEnabled" GroupName="RB2" Grid.Row="1" IsChecked="{Binding Path=Enabled, Mode=TwoWay}" Margin="10,0,0,0" VerticalAlignment="Bottom" Grid.Column="7" Content="Enabled" ></RadioButton>


<RadioButton Name="rdbtnDisabled" GroupName="RB2" IsChecked="{Binding Path=Disabled,Mode=TwoWay}" Grid.Row="2" Margin="10,0,0,0" VerticalAlignment="Top" Grid.Column="7" Content="Disabled" ></RadioButton>


<TextBlock FontWeight="Bold" Text="Public/Private" Grid.Row="0" Grid.Column="8" Height="27" Width="90" Name="txtblkPublicorPrivate"/>


<RadioButton Name="rdbtnPublic" GroupName="RB3" Grid.Row="1" IsChecked="{Binding Path=Public,Mode=TwoWay}" Margin="10,0,0,0" VerticalAlignment="Bottom" Grid.Column="8" Content="Public" ></RadioButton>


<RadioButton Name="rdbtnPrivate" GroupName="RB3" IsChecked="{Binding Path=Private,Mode=TwoWay}" Grid.Row="2" Margin="10,0,0,0" VerticalAlignment="Top" Grid.Column="8" Content="Private" ></RadioButton>


<TextBlock Grid.Row="3" Visibility="Collapsed" Text="{Binding FactorID,Mode=TwoWay}" Grid.Column="3" Height="20" Width="50"/>


<TextBlock Grid.Row="3" Visibility="Collapsed" Text="{Binding UserID,Mode=TwoWay}" Grid.Column="4" Height="20" Width="80"/>


<Button Name="btnsave" Grid.Row="4" Command="{Binding DataContext.SaveUDFactors,ElementName= FactorsListBox}" Grid.Column="4" Content="Save" Height="27" Width="60" Click="btnsave_Click" IsEnabled="{Binding DataContext.disableSave,ElementName= FactorsListBox, Mode=TwoWay}" />


<Button Name="btncancel" Grid.Row="4" Grid.Column="6" Content="Cancel" Height="27" Width="60" Command="{Binding DataContext.disablebtnSave,ElementName= FactorsListBox, Mode=TwoWay}" />


</Grid>


</Grid>


</StackPanel>


</DataTemplate>


</ListBox.ItemTemplate>


</ListBox>


</Grid>


</ContentControl>


</ScrollViewer.Content>


</ScrollViewer>


</StackPanel>


</Grid>


I am having like this listbox with some of the controls how to get the selected item with this in which the selected item should have one background color and all controls should be enable rest of the selected item should have another background colour and all controls should be in disable mode........
Ramachandran replied to Jitendra Faye on 12-Jun-12 01:14 AM

<Grid Grid.Row="1">


<StackPanel Style="{StaticResource CustomStackPanel}">


<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Height="300" Width="950">


<ScrollViewer.Content>


<ContentControl x:Name="groupofcontrols" IsEnabled="{Binding contentenableordisable}">


<Grid x:Name="groupofcontrolsgrid" Background="{Binding Path=Color}">


<ListBox Grid.Row="1" Name="FactorsListBox" ItemsSource="{Binding FactorsList,Mode=TwoWay}">


<ListBox.ItemTemplate>


<DataTemplate>


<StackPanel>


<Grid>


<Grid.RowDefinitions>


<RowDefinition Height="2*"/>


</Grid.RowDefinitions>


<Grid Grid.Row="0">


<Grid.ColumnDefinitions>


<ColumnDefinition Width="150"/>


<ColumnDefinition Width="140"/>


<ColumnDefinition Width="90"/>


<ColumnDefinition Width="100"/>


<ColumnDefinition Width="90"/>


<ColumnDefinition Width="30"/>


<ColumnDefinition Width="60"/>


<ColumnDefinition Width="120"/>


<ColumnDefinition Width="90"/>


</Grid.ColumnDefinitions>


<Grid.RowDefinitions>


<RowDefinition Height="40"/>


<RowDefinition Height="40"/>


<RowDefinition Height="40"/>


<RowDefinition Height="40"/>


<RowDefinition Height="40"/>


</Grid.RowDefinitions>


<TextBox Grid.Row="0" Margin="0,0,0,0" Text="{Binding Name,Mode=TwoWay}" IsEnabled="True" Grid.Column="0" Height="27" Width="100" HorizontalAlignment="Left" Name="txtName"/>


<TextBox Grid.Row="1" Grid.Column="0" Width="150" Height="70" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" Name="txtdesc" Text="{Binding Description, Mode=TwoWay}" Grid.RowSpan="2" AcceptsReturn="True"/>


<TextBlock FontWeight="Bold" Grid.Row="0" Grid.Column="1" Height="27" Margin="0,0,0,0" Width="130" Text="Impacts or Revenue" Name="txtblkImpactsorRevenue"/>


<RadioButton Name="rdbtnImpacts" GroupName="RB1" Grid.Row="1" Margin="10,0,0,0" IsChecked="{Binding Path=Impact,Mode=TwoWay}" VerticalAlignment="Bottom" Grid.Column="1" Content="Impacts" ></RadioButton>


<RadioButton Name="rdbtnRevenue" GroupName="RB1" Grid.Row="2" Margin="10,0,0,0" IsChecked="{Binding Path=Revenue,Mode=TwoWay,ElementName=FactorsListBox}" VerticalAlignment="Top" Grid.Column="1" Content="Revenue" ></RadioButton>


<TextBlock FontWeight="Bold" Text="Months Applied To" Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="2" Height="27" HorizontalAlignment="Center" Width="190" Name="txtblkMonthsAppliedTo"/>


<TextBlock Name="txtblkFrom" Grid.Row="1" Grid.Column="2" Width="90" Text="From" Height="27"/>


<TextBlock Name="txtblkTo" Grid.Row="2" Grid.Column="2" Width="90" Text="To" Height="27"/>


<ComboBox Name="ddlfrommonth" Width="90" Grid.Row="1" Grid.Column="3" Height="27"

ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ScrollViewer}, Path=DataContext.FromMonthList}"

DisplayMemberPath="MonthName"

SelectedValuePath="MonthName"

SelectedValue="{Binding FromMonth,Mode=TwoWay}"/>


<ComboBox Name="ddlTomonth" Width="90" Grid.Row="2" Grid.Column="3" Height="27"

ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ScrollViewer}, Path=DataContext.ToMonthList}"

DisplayMemberPath="MonthName"

SelectedValuePath="MonthName"

SelectedValue="{Binding ToMonth,Mode=TwoWay}" />


<TextBlock FontWeight="Bold" Text="Max%Min%" Grid.Row="0" Grid.Column="4" Height="27" Width="90" Name="txtblkMaxMinPercent"/>


<TextBox Name="MaxPercen" KeyDown="MaxPercen_KeyDown" MaxLength="3" Grid.Row="1" Grid.Column="4" Width="90" Height="27" DataContext="{StaticResource VM}" Text="{Binding MaxperValue,Mode=TwoWay}"/>


<TextBox Name="MinPercen" MaxLength="2" Grid.Row="2" KeyDown="MinPercen_KeyDown" Grid.Column="4" Width="90" Height="27" DataContext="{StaticResource VM}" Text="{Binding MinperValue,Mode=TwoWay}"/>


<Slider Name="WireFrameSlider" Orientation="Vertical" Minimum="{Binding MinperValue,Mode=TwoWay}" Maximum="{Binding MaxperValue,Mode=TwoWay}" DataContext="{StaticResource VM}" Value="{Binding zoomslider,Mode=TwoWay}" Height="120" Grid.Row="0" Grid.Column="5" Grid.RowSpan="3" Width="30" Cursor="Arrow"></Slider>


<TextBlock FontWeight="Bold" Text="Effect" Grid.Row="0" Grid.Column="6" Height="27" Width="90" Name="txtblkEffect"/>


<TextBlock Text="{Binding ElementName=WireFrameSlider,Path=Value,Mode=TwoWay}" Grid.Row="1" Grid.Column="6" Height="27" Width="90" Name="txtblkEffectPercentValue"/>


<TextBlock FontWeight="Bold" Text="Enabled/Disabled" Grid.Row="0" Grid.Column="7" Height="27" Width="120" Name="txtblkEnabledorDisabled"/>


<RadioButton Name="rdbtnEnabled" GroupName="RB2" Grid.Row="1" IsChecked="{Binding Path=Enabled, Mode=TwoWay}" Margin="10,0,0,0" VerticalAlignment="Bottom" Grid.Column="7" Content="Enabled" ></RadioButton>


<RadioButton Name="rdbtnDisabled" GroupName="RB2" IsChecked="{Binding Path=Disabled,Mode=TwoWay}" Grid.Row="2" Margin="10,0,0,0" VerticalAlignment="Top" Grid.Column="7" Content="Disabled" ></RadioButton>


<TextBlock FontWeight="Bold" Text="Public/Private" Grid.Row="0" Grid.Column="8" Height="27" Width="90" Name="txtblkPublicorPrivate"/>


<RadioButton Name="rdbtnPublic" GroupName="RB3" Grid.Row="1" IsChecked="{Binding Path=Public,Mode=TwoWay}" Margin="10,0,0,0" VerticalAlignment="Bottom" Grid.Column="8" Content="Public" ></RadioButton>


<RadioButton Name="rdbtnPrivate" GroupName="RB3" IsChecked="{Binding Path=Private,Mode=TwoWay}" Grid.Row="2" Margin="10,0,0,0" VerticalAlignment="Top" Grid.Column="8" Content="Private" ></RadioButton>


<TextBlock Grid.Row="3" Visibility="Collapsed" Text="{Binding FactorID,Mode=TwoWay}" Grid.Column="3" Height="20" Width="50"/>


<TextBlock Grid.Row="3" Visibility="Collapsed" Text="{Binding UserID,Mode=TwoWay}" Grid.Column="4" Height="20" Width="80"/>


<Button Name="btnsave" Grid.Row="4" Command="{Binding DataContext.SaveUDFactors,ElementName= FactorsListBox}" Grid.Column="4" Content="Save" Height="27" Width="60" Click="btnsave_Click" IsEnabled="{Binding DataContext.disableSave,ElementName= FactorsListBox, Mode=TwoWay}" />


<Button Name="btncancel" Grid.Row="4" Grid.Column="6" Content="Cancel" Height="27" Width="60" Command="{Binding DataContext.disablebtnSave,ElementName= FactorsListBox, Mode=TwoWay}" />


</Grid>


</Grid>


</StackPanel>


</DataTemplate>


</ListBox.ItemTemplate>


</ListBox>


</Grid>


</ContentControl>


</ScrollViewer.Content>


</ScrollViewer>


</StackPanel>


</Grid>


I am having like this listbox with some of the controls how to get the selected item with this in which the selected item should have one background color and all controls should be enable rest of the selected item should have another background colour and all controls should be in disable mode........