How to dynamically create buttons in panorama items? - silverlight

I have String array. It looks something like:
I have an empty panorama:
<controls:Panorama x:Name="MainPanorama">
</controls:Panorama>
I want to dynamically, using my array, create panorama page with panorama items,
it should be like:
<controls:Panorama x:Name="MainPanorama">
<controls:PanoramaItem Header="first">
<ScrollViewer>
<StackPanel>
<Button Name="button1" content="create"/>
<Button Name="button2" content="save"/>
<Button Name="button3" content="open"/>
</StackPanel>
</ScrollViewer>
</controls:PanoramaItem>
<controls:PanoramaItem Header="second">
<ScrollViewer>
<StackPanel>
<Button Name="button4" content="save as"/>
<Button Name="button5" content="import"/>
</StackPanel>
</ScrollViewer>
</controls:PanoramaItem>
</controls:Panorama>
So, the question is: how to dynamically create buttons in panorama items? Buttons must have different names.
p.s: The array that I show here is not the actual one. In my work I have another larger one, but I think it's enough to understand the problem.

You can use items controls for this goal (ListBox, or etc): bind a string array as item source and set buttons as item template. Please read more about controls and binding in Silverlight.

Related

Create user control in wpf

Please can anyone tell me how to make a combobox which contains items as a label controls and each label control has delete button on its left side. If click on delete button this item will be deleted and added to the listbox.
I have tried this but I am not able to do this.
*The Below code is the exactly doing the same. *
<ComboBox Width="100" Height="23">
<ComboBoxItem>
<StackPanel Orientation="Horizontal">
<Button Content="Delete" Click="Button_Click" Name="btn"/>
<Label Foreground="Red">Red</Label>
</StackPanel>
</ComboBoxItem>
</ComboBox>

Pull specific control out of a silverlight listbox given a random index into the list?

I have a listbox which uses the following template to bind a list of clips. I would like to pull one of those clips at random and have it play. That is the easy part and is pretty straight forward. I would then like to make an associated image visible. I can't seem to find out how to get the controls associated with the index in the listbox. Any ideas.
`
<StackPanel Margin="0,0,0,12" Width="420">
<Button Click="Button_Click" Background="{StaticResource PhoneAccentBrush}"
Height="90" Width="420" x:Name="btn">
<StackPanel Orientation="Horizontal" >
<Image x:Name="Selected" Width="75" Source="Images/Playing.png" Visibility="Collapsed" />
<TextBlock Text="{Binding Name}" TextWrapping="NoWrap" Width="330" VerticalAlignment="Center"
Style="{StaticResource PhoneTextLargeStyle}" />
</StackPanel>
</Button>
</StackPanel>
</DataTemplate>
`
This is a ListBox so have a list of objects bound to its ItemsSource. Add a new "NowPlaying" property to the class of these objects. Bind this property to the Visibility property of the "Selected" Image via a BoolToValueConverter
Now you just toggle that new property and need not delve in the Visual Tree to do what the binding system should be doing for you
To find the conrols that have been generated from your template you can use the methods on ItemContainerGenerator. For example, if you want to find the elements at index '5', do the following:
ListBoxItem lbi = listBox.ItemContainerGenerator.ContainerFromIndex(5) as ListBoxItem;
There are various other methods that allows you to find the container given a bound model item for example.
You can then navigate the visual tree, (I wrote a helper called Linq-to-VisualTree that makes this a bit easier) to locate the elements you require. To find the image:
var image = lbi.Descendants<Image>().Single() as Image;

How to make a WPF TextBox fill all available space between two buttons ?

I'm trying to make a WPF TextBox fill all available space between two buttons. For some reason the code below does not what I'm trying to achieve
<DockPanel Height="48" LastChildFill="False">
<Button DockPanel.Dock="Left">
<Image Source="Images\large_load.png"></Image>
</Button>
<Button DockPanel.Dock="Left">
<Image Source="Images\large_reload.png"></Image>
</Button>
<TextBox Height="24" HorizontalAlignment="Stretch" DockPanel.Dock="Left"></TextBox>
<Button DockPanel.Dock="Right" Width="48">
<Image Source="Images\large_delete.png"></Image>
</Button>
</DockPanel>
The TextBox is not stretched.
Another problem is that when text is added, the textbox width increases and eventually it pushes the right button out of the visible space.
Don't set LastChildFill to false and make the TextBox the last child (by moving the element to the bottom in the code).
(Or use a proper control, like a Grid)

WPF: Visual studio like error buttons

I want to get this.
buttons http://www.shrani.si/f/X/6Y/24Jhn9D3/buttns.png
Everything works so far, buttons act as filter and are bind to the grid control.
All i want is the icons and counter on the button.
Whats the correct way of implementing those?
<ToggleButton x:Name="IsErrorShown" Margin="4" Width="100" Content="{lex:LocText Errors, Assembly=Client}">
I have tried adding image like this:
<ToggleButton x:Name="IsErrorShown" Margin="4" Width="100" Content="{lex:LocText Errors, Assembly=Client}">
<StackPanel>
<Image Source="Resources/Warning"/>
</StackPanel>
</ToggleButton>
but i get error that prop. Content is defined more then once.
A WPF Button (or ToggleButton) is a content control, into which you can put anything.
I haven't checked, but these buttons probably have a horizontal stack panel or a DockPanel, with an Image and then one or two TextBlocks. You could make a template for these, and also use binding to set the TextBlock Text content from your viewmodel.
Snoop ( http://snoopwpf.codeplex.com/ ) is a great tool for finding out how other people have built things in WPF.
The Adam Nathan WPF book is excellent, and if you don't have it you should get it. http://www.amazon.co.uk/Windows-Presentation-Foundation-Unleashed-WPF/dp/0672328917
Here's an example:
<ToggleButton Height="24" Width="100">
<DockPanel>
<Image Source="c:\\temp\\me.jpg" Margin="3"/>
<TextBlock Text="20 Errors"/>
</DockPanel>
</ToggleButton>

Button in a WPF ListItem

I'm a bit new to WPF. I am working on a list UI where each item in the list will have a set of corresponding buttons to operate on that particular data item.
Coming from a web background, I normally would have bound the value into a hidden element in that particular list item or something. However, I just need to find the corresponding technique in this WPF world :-)
The most common technique is to use templates. Please consider using my example of a templated ListItem (for example ListBoxItem):
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Command="{Binding Path=YourCommand}" Content="Dynamic Button 1" />
<Button Command="{Binding Path=YourSecondCommand}" Content="Dynamic Button 2" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Please feel free to ask if you have any questions/ideas.

Resources