I set ObservableCollection<Employe> as source for ListBox.ItemsSource, and set some DataTemplate for ListBox.ItemsSource.
XAML for template:
<DataTemplate x:Key="tmpEmploye">
<Border BorderThickness="3" BorderBrush="Gray" CornerRadius="5">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Surname}"
HorizontalAlignment="Stretch" Margin="2"
FontWeight="Bold" TextWrapping="Wrap"/>
<TextBlock Text="{Binding Path=Name}"
HorizontalAlignment="Stretch" Margin="2"/>
<TextBlock Text="{Binding Path=Patronymic}"
HorizontalAlignment="Stretch" Margin="2"
TextWrapping="Wrap"/>
</StackPanel>
<TextBlock Text="{Binding Path=Post}" Foreground="Gray"
HorizontalAlignment="Stretch" Margin="2"
FontStyle="Italic" TextWrapping="Wrap"/>
</StackPanel>
</Border>
</DataTemplate>
XAML for ListBox:
<ListBox x:Name="lbEmployees" x:FieldModifier="internal"
ItemTemplate="{StaticResource tmpEmploye}" HorizontalContentAlignment="Stretch"/>
Result:
Look, the Employee class has a Post property, which is an index of record from ObservableDictionary<Int64, String> (my some class). You can see the '-1' values, but I need to get value for this key from my ObservableDictionary<Int64, String> object. Can I get necessary values and to display it through XAML code?
You can write a IMultiValueConverter, and bind both the Post property and your dictionary to it.
See http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx
Related
I have listBox and i want to see the selected item ftom the listBox on some textBox.
(the Collection is a list of string)
I trying to write the code but this is not working.
<ListBox x:Name="Collection__" Grid.Row="0" ItemsSource="{Binding Collection}" />
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Vertical" Margin="5">
<DockPanel>
<TextBlock DockPanel.Dock="Left" Text="Collection name:"/>
<TextBox DockPanel.Dock="Left" Margin="5,0" Text="{Binding ElementName=Collection__, Path=SelectedItem}"/>
</DockPanel>
</StackPanel>
found it ...
<StackPanel>
<ListBox x:Name="ElementListBox" ItemsSource="{Binding Elements}"
IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox Text="{Binding Elements/Name}"/>
</StackPanel>
I have code like this in my WPF view ..
<!--Column 2-->
<Border Grid.Column="1" Grid.Row="1" >
<TextBox x:Name="textB2BR0K2" Text="{Binding Path=ListBlok2B[0].B2B1}" />
</Border>
<TextBox x:Name="textB2BR1AK2" Grid.Column="1" Grid.Row="4" Text="{Binding Path=ListBlok2B[0].B2BR1A}"/>
<TextBox x:Name="textB2BR1BK2" Grid.Column="1" Grid.Row="5" Text="{Binding Path=ListBlok2B[0].B2BR1B}"/>
<TextBox x:Name="textB2BR1CK2" Grid.Column="1" Grid.Row="6" Text="{Binding Path=ListBlok2B[0].B2BR1C}"/>
<!--Column 3-->
<Border Grid.Column="2" Grid.Row="1" >
<TextBox x:Name="textB2BR0K3" Text="{Binding Path=ListBlok2B[1].B2B1}" />
</Border>
<TextBox x:Name="textB2BR1AK3" Grid.Column="2" Grid.Row="4" Text="{Binding Path=ListBlok2B[1].B2BR1A}"/>
<TextBox x:Name="textB2BR1BK3" Grid.Column="2" Grid.Row="5" Text="{Binding Path=ListBlok2B[1].B2BR1B}"/>
<TextBox x:Name="textB2BR1CK3" Grid.Column="2" Grid.Row="6" Text="{Binding Path=ListBlok2B[1].B2BR1C}"/>
And then I have fill this field from my database from my collection and work properly. But when I try to change one of the value all the data that have some binding attributes changes at the same time
ListBlok2B[0].B2BR1A and ListBlok2B[1].B2BR1A changes at the same time when I try to edit inside it..
Anyone knows how to solve this? I want it to bind in different index collection that I have so I can insert it to my database context.Blok2Bs.insertAllOnSubmit(ListBlok2B)
why dont you use an itemscontrol and bind your collection to it?
<ItemsControl ItemsSource="{Binding ListBlok2B}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Border Grid.Column="1" Grid.Row="1" >
<TextBox x:Name="textB2BR0K2" Text="{Binding Path=B2B1}" />
</Border>
<TextBox Grid.Column="1" Grid.Row="4" Text="{Binding Path=B2BR1A}"/>
<TextBox Grid.Column="1" Grid.Row="5" Text="{Binding Path=B2BR1B}"/>
<TextBox Grid.Column="1" Grid.Row="6" Text="{Binding Path=B2BR1C}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I have spent wayyyy too much time on this, so if someone can help point me in the right direction, I would be very appreciative.
I have an Expander with a ListBox in it. I have bound the ListBox to a List ie FilteredProjects. I would like the Header of the expander to reflect the number of items in the displayed list. I did implement INotifyPropertyChanged for ProjectsCount, and the ItemsSource for the ListBox updates just fine. Thanks in advance for your time.
<Expander>
<Expander.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Projects" />
<Border CornerRadius="4" Padding="0" BorderThickness="1" BorderBrush="Black" Background="#FF545F42" Margin="1,0,0,0" >
<Label Content="{Binding Path=ProjectsCount, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource filter}}" Padding="0" FontSize="8" FontStyle="Italic"></Label>
</Border>
</StackPanel>
</DataTemplate>
</Expander.HeaderTemplate>
<ListBox x:Name="ProjectsFilterListView" Opacity="1" ItemsSource="{Binding FilteredProjects}" >
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}" Content="{Binding ItemName}" Margin="10,10,0,10"></CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Expander>
You could just get the Item Count directly from the ListBox
<Label Content="{Binding Path=Items.Count, ElementName=ProjectsFilterListView}" />
Here is an example of it working, I replaced Label for TextBlock so I could use StringFormat to add "Items:" text before the number as I am not sure what your Converter is doing
<StackPanel Orientation="Horizontal">
<TextBlock Text="Projects" />
<Border CornerRadius="4" Padding="0" BorderThickness="1" BorderBrush="Black" Background="Red" Margin="1,0,0,0" >
<TextBlock Text="{Binding Path=Items.Count, ElementName=ProjectsFilterListView, StringFormat={} Items: {0}}" Padding="0" FontSize="10" FontStyle="Italic" Foreground="Black"/>
</Border>
</StackPanel>
Result:
My listbox XAML looks like this:
<ListBox x:Name="lstbxbProducts" SelectionChanged="lstbxbProducts_SelectionChanged_1" HorizontalAlignment="Left" Height="547" Margin="0,221,0,0" VerticalAlignment="Top" Width="1044" RenderTransformOrigin="0.600000023841858,0.5">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel Width="80" VerticalAlignment="Center" Orientation="Horizontal">
<TextBlock Text="{Binding prdnum}" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
<TextBlock Text=" -" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Width="400">
<TextBlock Text="{Binding prddsc}" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Width="180">
<StackPanel>
<TextBlock Text="{Binding um_cod}" Name="txtblkUmcode" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Width="20"></StackPanel>
<StackPanel Width="20">
<Image Source="/Images/Arrowselection.png" Tapped="Image_Tapped_1" VerticalAlignment="Center" ></Image>
</StackPanel>
</StackPanel>
<StackPanel Width="180">
<TextBlock Text="{Binding prcby_prc}" VerticalAlignment="Center" HorizontalAlignment="Center" ></TextBlock>
</StackPanel>
<StackPanel Width="100">
<TextBox Text="{Binding stdordqty, Mode=TwoWay}" VerticalAlignment="Center" TextAlignment="Center" HorizontalAlignment="Right" ></TextBox>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to set the text of Name="txtblkUmcode" present in my listbox from c# codebhind in this way, but no luck:
TextBlock txtBlk = new TextBlock();
txtBlk = lstbxbProducts.FindName("txtblkUmcode") as TextBlock;
txtBlk .Text = "test";
Please let me know how can I:
set the text of that textblock prsent inside a datatemplate of a my listbox ?
i want to change the text of that textblock of that single listbox item only ?
How can I do this? Please let me know.
You have to avoid this kind of programming practice because if the view changes - for example there comes a modified DataTemplate with other controls displaying that property(Label, TextBox, etc) - can break your application.
Sometimes there is no other solution.
See the answer here: http://msdn.microsoft.com/en-us/library/bb613579.aspx
I have created one datatemplate resouce in my xaml file
<navigation:Page.Resources>
<DataTemplate x:Key="PageFooter" >
<StackPanel Width="{Binding Path=UsablePageWidth, Mode=OneWay}" Height="Auto" x:Name="spFooter" HorizontalAlignment="Center">
<TextBlock x:Name="txtParameter" FontSize="16" Text="{Binding}"
FontWeight="Bold" Foreground="White"
HorizontalContentAlignment="Center"
Width="{Binding Path=UsablePageWidth, Mode=OneWay}"
Background="Black" Height="35" />
</StackPanel>
</DataTemplate>
</navigation:Page.Resources>
Now in my code behind i want to update this Textblock with my database value
How to do this? I am new in silverlight