Can't Use combobox in C# code - c#

I want to add itens to a combobox but in c# because i dont want to add all the time the same comboboxitens. My code is this to the combobox i want to add itens.
<ScrollViewer Margin="252,130,296,134" Grid.Row="1" VerticalAlignment="Top" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollMode="Enabled" ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.ZoomMode="Disabled">
<StackPanel >
<ItemsControl x:Name="ic" Grid.Row="2">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Left" VerticalAlignment="Center">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Border BorderBrush="#C83245" Background="White" BorderThickness="1">
<ComboBox x:Name="cbSeletion" VerticalAlignment="Center" FontSize="14" Width="250" Height="40" Foreground="Black" Tapped="cbSeletion_Tapped">
</ComboBox>
</Border>
<Border BorderBrush="#C83245" Background="White" BorderThickness="1">
<TextBlock Text="{Binding Name}" FontSize="14" VerticalAlignment="Center" Width="350" Foreground="Black"/>
</Border>
<Border BorderBrush="#C83245" Background="White" BorderThickness="1" >
<TextBlock Text="{Binding Position}" FontSize="14" VerticalAlignment="Center" Width="250" Foreground="Black"/>
</Border>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
what can i do to use it?

You can bind the combobox ItemsSource to an ObservableCollection in your viewmodel:
<ComboBox ItemsSource="{Binding Items}"/>
The property:
ObservableCollection<string> _items = new ObservableCollection<string>();
public ObservableCollection<string> Items
{
get
{
return _items;
}
}
You can then add items to and from that collection and the items in the dropdown will reflect it.

If you have really few items, you could directly use the ComboBoxItem in XAML, but still it is not that ellegant. The MVVM approach is definately the answer, especially for many items in the collection of the ComboBox, cause it gives you greater flexibility and the logic is not that hard-coded.

Related

Wpf Binding Dicionaly with key and list on objects to ListView [duplicate]

I have a Dictionary<string, List<WritingMachine>> where the key is the hostname of a machine that writes to a server, and the value is an list of WritingMachine objects (shown below):
public class WritingMachine
{
public string HostName { get; set; }
public string Program { get; set; }
public string Login { get; set; }
public string Server { get; set; }
}
I'm trying to bind the dictionary to a WPF ListBox; displaying the Hostname (the dictionary's key), and a summary of the WritingMachine list that's associated with that key.
I'm having trouble accessing the properties of the individual WritingMachine elements within the list.
Here's my XAML so far:
<ListBox x:Name="MachineListBox" Margin="10" Grid.Column="0" FontFamily="Consolas" ItemsSource="{Binding Path=MachineDictionary}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<ContentPresenter Content="{Binding Key}" Margin="0,0,4,0"/>
<ItemsControl ItemsSource="{Binding Value}" Margin="10,0,0,0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This results in a listbox that looks like this:
Obviously this is wrong because the WritingMachine list elements are just returning the default ToString. By overriding WritingMachine's ToString I can get the effect I want (more or less):
But this a crap way of doing it... I want to be able to access the individual element properties and arrange them in controls in my ListBox.ItemTemplate using Content="{Binding Value.Server}" or similar.
Any ideas?
For your ItemsControl you need to set an ItemTemplate:
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock>Server: </TextBlock>
<TextBlock Text="{Binding Server}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
That will get you started.
Set DataContext property for inner control to show WritingMachine properties.
Like so :
<StackPanel DataContext="{Binding Value}" Grid.Row="1">
This will change the DataContext to List<WritingMachine> for inner display control, where you can do normal binding. See full XAML code below :
<ListBox x:Name="LbxMachineDictionary" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<Grid Width="461" Height="199">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding Key}" VerticalAlignment="Top"/>
<StackPanel DataContext="{Binding Value}" Grid.Row="1">
<TextBlock HorizontalAlignment="Left" Grid.Row="1" TextWrapping="Wrap" Text="{Binding HostName}" VerticalAlignment="Top"/>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Program}" VerticalAlignment="Top"/>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Login}" VerticalAlignment="Top"/>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Server}" VerticalAlignment="Top"/>
</StackPanel>
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

LongListSelector's performance is very bad. How to improve?

here is my code
<phone:LongListSelector ItemsSource="{Binding MovieTimeInDetailItems}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Margin="24,0,0,12">
<TextBlock Text="{Binding Theater}" FontSize="34"/>
<ListBox ItemsSource="{Binding TimeItems}" HorizontalAlignment="Stretch" VerticalAlignment="Top" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="115" Margin="0,0,0,0">
<TextBlock Text="{Binding Time}"
TextWrapping="Wrap"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontSize="26"
Foreground="{StaticResource PhoneSubtleBrush}"/>
<Border Margin="92,0,0,0" HorizontalAlignment="Left" Width="{Binding Width}" Height="25" BorderThickness="1.5,0,0,0" BorderBrush="{StaticResource PhoneSubtleBrush}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
In each LLS item contains a Listbox which display time list.
my question is ...
Can I display a time list without Listbox ? It seems Listbox cause the low performance.

Fill panorama item dynamically in wp7

I dynamically want to add panorama items in my application. No. of item(3 to 7) is depend on the json response I am getting. Presently for testing I created 4 items in xaml and it works for me but its not dynamic. Here is my xaml.
<Grid x:Name="LayoutRoot">
<controls:Panorama Title="my panorama" Loaded="Panorama_Loaded" Name="title1" ItemsSource="{Binding}">
<controls:Panorama.Background>
<ImageBrush ImageSource="/Images/Panaroma_BG.png"/>
</controls:Panorama.Background>
<!--Panorama item one-->
<controls:PanoramaItem Header="item1" Name="dashboard1" HeaderTemplate="{StaticResource DashBoardName}">
<Grid>
<ListBox Height="512" HorizontalAlignment="Left" Margin="6,8,0,0" Name="listBox1" VerticalAlignment="Top" Width="403" Tap="listBox1_Tap">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Rectangle Height="100" Width="400" HorizontalAlignment="Left" Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top"/>
<StackPanel Orientation="Horizontal">
<Image Width="100" Height="100" Source="{Binding Image}" Stretch="Fill" HorizontalAlignment="Left" VerticalAlignment="Top" />
<StackPanel Orientation="Horizontal"
Height="132">
<StackPanel Width="300" HorizontalAlignment="Left">
<Grid>
<TextBlock Text="{Binding CurrentValue}" HorizontalAlignment="Left" FontSize="25" />
<Image Width="20" Height="20" Source="{Binding SubImage}" Stretch="Fill" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
<TextBlock Text="{Binding PreviousValue}" HorizontalAlignment="Right" VerticalAlignment="Top" FontSize="15" />
</Grid>
<StackPanel Width="290" HorizontalAlignment="Right" VerticalAlignment="Stretch" Orientation="Vertical">
<TextBlock Text="{Binding ItemName}" FontSize="20" TextWrapping="Wrap" Width="290" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
</StackPanel>
</StackPanel>
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PanoramaItem>
<!--Panorama item two-->
<!--Panorama item three-->
<!--.................-->
</controls:Panorama>
</Grid>
I don't want to write xaml for Panorama item two, Panorama item three and so on I think there must be some way to do it at runtime. Please help me out.
There must be some template like thing which I can use. and then fill the inner items like CurrentValue , ItemName etc. through code
I have tested below code for pivot. You can implement similar for Panaroma
Dim objPivotItem As PivotItem
Dim sScrollViewer As ScrollViewer
Dim sStackPanel As StackPanel
Dim textHeader As TextBlock
Dim txtContents As RichTextBox
For i = 0 to <Condition>
objPivotItem = New PivotItem
sStackPanel = New StackPanel
sScrollViewer = New ScrollViewer
textHeader = New TextBlock
txtContents = New RichTextBox
'Set TextHeader properties
'Set TxtContents properties
objPivotItem.Content = sScrollViewer
sScrollViewer.Content = sStackPanel
sStackPanel.Children.Add(textHeader)
sStackPanel.Children.Add(txtContents)
objPivot.Items.Add(objPivotItem
)
Next i
You actually can set an ItemsSource property for the whole panorama control, e. g.:
<phone:Panorama x:Name="MyPanorama">
<phone:Panorama.ItemTemplate>
<DataTemplate>
<phone:PanoramaItem Header="MyHeader">
<TextBlock Text="{Binding Text}"/>
</phone:PanoramaItem>
</DataTemplate>
</phone:Panorama.ItemTemplate>
<phone:Panorama.HeaderTemplate>
<DataTemplate>
<TextBlock Visibility="Collapsed" />
</DataTemplate>
</phone:Panorama.HeaderTemplate>
</phone:Panorama>
and then, for example from the code behind:
MyPanorama.ItemsSource = myItemsCollection;

Stretching of Border's width for ListBoxItem

I have wrote some class, named as Employe. Employees collection I set as source for ListBox WPF control. I have wrote such template for ItemTemplate:
<ResourceDictionary>
<DataTemplate x:Key="tmpEmploye">
<Border BorderThickness="3" BorderBrush="Gray" CornerRadius="5"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<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>
</ResourceDictionary>
Each item has border. The border must be expanded according width of ListBox. I set HorizontalAlignment="Stretch" for Border, but it is not occured as I want.
How can I correct it?
Try this:
<ListBox Name="lbEmployees" ItemTemplate="{StaticResource tmpEmploye}"
HorizontalContentAlignment="Stretch"
/>

how to acess on controls inside of stackpanel in windows phone7?

i design page bellow code.
<ScrollViewer VerticalScrollBarVisibility="Visible" Grid.Row="1" x:Name="svProduct">
<StackPanel>
<ItemsControl x:Name="lstSearchResult" ItemsSource="{Binding Path=PIProductList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Width="480" Style="{Binding CellStyle}" Orientation="Horizontal" VerticalAlignment="Center" Height="50" >
<TextBlock Foreground="Black" FontSize="20" Width="320" FontFamily="Tahoma" Margin="10,0,0,0" Text="{Binding Title}" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>
<Button Name="btnBookmark" Click="btnBookmark_Click" Tag="{Binding}" Background="Transparent">
<Button.Content>
<Image Source="/Images/bookmarks_red.png" Width="33" Height="30" VerticalAlignment="Top" Margin="-15"></Image>
</Button.Content>
</Button>
<Button BorderThickness="0" x:Name="btnSubmit" Click="btnSubmit_Click" Background="Transparent" Tag="{Binding}" >
<Button.Content>
<Image Name="ram" Source="/Images/blue_arrow.png" Width="40" Height="40" VerticalAlignment="Top" Margin="-15"></Image>
</Button.Content>
</Button>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
i want to access for btnBookmark visuble false .
can't access btnBookmark.Visibility=Visibility.collapsed
how to do this?
please help to me...........
The best way I know to do this is to create a Visiblity property on your item ViewModel (the one that is bound to each row in your ItemsControl) and toggle that value based on the changes to each item, presumably via the toggle button in each row. I don't know of a good way to "loop and look" for these internal controls. You're much better off using the existing data binding infrastructure to manage this for you.

Categories

Resources