Adding Item to listpicker by loop - c#

I want to add items to listpicker control of toolkit.
I am doing this way.
for (int i = 0; i < cstringl.Length; i++)
{
listPickerCountrySignup.Items.Add(cstringl[i]);
}
and here is MY XAML.
<toolkit:ListPicker x:Name="listPickerCountrySignup" SelectionChanged="listPickerCountry_SelectionChanged" Height="72" HorizontalAlignment="Left" Margin="14,43,0,0" VerticalAlignment="Top" Width="436" FullModeHeader="Select Country" Background="White" BorderBrush="White" Foreground="{StaticResource listPickerBrush}" Style="{StaticResource ListPickerStyle1}">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Country}" Width="300" />
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Country}" Width="300" Margin="0,0,0,20" FontSize="24"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
But its not showing in UI as I have binding in XAML but item I am adding from code behind by loop. No item source binding. How I Can Show Item in That List..

You have to do some data binding.
ObservableCollection<T> ListPickerItems = new ObservableCollection<T>();
for (int i = 0; i < cstringl.Length; i++)
{
ListPickerItems .Add(cstringl[i]);
}
in Xaml:
<toolkit:ListPicker ItemsSource={Binding ListPickerItems} ... />

Related

How to delete an object from a ListView using a button in the ListViewItem?

Write a text in the inputTitle TextBox and press the Add button to add item to the TaskList ListView.
public MainWindow()
{
InitializeComponent();
DataContext = new AddItem();
newItems = new ObservableCollection<AddItem> { };
taskList.ItemsSource = newItems;
}
private void addButton_Click(object sender, RoutedEventArgs e)
{
var newItem = new AddItem()
{
Title = inputTitle.Text,
};
newItems.Add(newItem);
taskList.SelectedItem = newItem;
}
private void deleteButton_Click(object sender, RoutedEventArgs e)
{
}
It is to add item to TaskList by pressing the Add button, and I want to delete it by pressing Del button.
<Canvas Margin="0,0,0,10">
<Canvas Height="81" Canvas.Left="10" Canvas.Top="17" Width="380">
<TextBlock x:Name="textBlock" Text="TaskTitle" TextWrapping="Wrap" Height="24" Width="380" FontSize="20" IsEnabled="False"/>
<TextBox x:Name="inputTitle" Text="{Binding Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap" Width="320" FontSize="24" Canvas.Top="27" Height="44" TabIndex="1" Canvas.Left="4" Margin="2,2,2,2"/>
<Button x:Name="addButton" Content="Add" Canvas.Left="333" Canvas.Top="32" Height="34" Width="34" Click="addButton_Click" IsEnabled="{Binding CanAdd, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
</Canvas>
<ListView x:Name="taskList" Height="500" Width="400" Canvas.Top="116">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox >
<CheckBox.LayoutTransform>
<ScaleTransform ScaleX="2.5" ScaleY="2.5"/>
</CheckBox.LayoutTransform>
</CheckBox>
<TextBlock Text="{Binding Title}" FontSize="30" Width="290"/>
<Button x:Name="deleteButton" Content="Del" Height="34" Width="34" Click="deleteButton_Click"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Canvas>
You can bind the Tag property of Button to the current item.
<ListView x:Name="taskList" Height="500" Width="400" Canvas.Top="116" ItemsSource="{Binding newItems}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox >
<CheckBox.LayoutTransform>
<ScaleTransform ScaleX="2.5" ScaleY="2.5"/>
</CheckBox.LayoutTransform>
</CheckBox>
<TextBlock Text="{Binding Title}" FontSize="30" Width="290"/>
<Button x:Name="deleteButton" Content="Del" Height="34" Width="34" Click="deleteButton_Click" Tag="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
As you bind the Tag property to your current item, you can use that to remove it from the collection.
private void ButtonBase_OnClickDel(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
var item = (AddItem)button.Tag;
newItems.Remove(item);
}

How to display items inside listbox in dinamically created tab item

I have data stored in IGroupping field of my repository class. I want to create tab item for each group of this collection, showing it`s items in listbox.
This is my code for creating tab items:
foreach (var group in repository.Products)
{
var tab = new MyTabItem();
tab.Header = group.Key;
tab.Products = group.AsEnumerable().ToList();
tab.FontSize = 16;
TabControlProducts.Items.Add(tab);
}
TabControlProducts.SelectedIndex = 0;
Here`s part of my xaml file:
<TabControl Grid.Row="1" Grid.Column="1" x:Name="TabControlProducts">
<TabControl.ContentTemplate>
<DataTemplate>
<ListBox FontSize="16" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyTabItem}}, Path=Products}" Visibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Code.MainGroup}"></TextBlock>
<TextBlock Text="{Binding Path=Code.MainForm}"></TextBlock>
<TextBlock Text="{Binding Path=Code.SubForm}"></TextBlock>
<TextBlock Text="{Binding Path=Code.Finishing}"></TextBlock>
<TextBlock Text="{Binding Path=Code.MaterialGroup}"></TextBlock>
<TextBlock Text="{Binding Path=Code.MaterialSort}"></TextBlock>
<TextBlock Text="{Binding Path=Code.Attest}"></TextBlock>
<TextBlock Text="{Binding Path=Code.Sizing}"></TextBlock>
<TextBlock Text="{Binding Path=Code.SubSizing}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
And declaration of MyTabItem class:
public class MyTabItem : TabItem
{
public List<Product> Products;
}
Now I have listboxes displayed in each of tab items, but they are empty
It's probably easier to bind directly to your product class and let the TabControl wrap each one in a TabItem.
foreach (var group in repository.Products)
{
TabControlProducts.Items.Add(group);
}
TabControlProducts.SelectedIndex = 0;
Then the xaml
<TabControl Grid.Row="1" Grid.Column="1" x:Name="TabControlProducts">
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="Header" Value="{Binding Key}"/>
</Style>
</TabControl.Resources>
<TabControl.ContentTemplate>
<DataTemplate>
<ListBox FontSize="16" ItemsSource="{Binding .}" Visibility="Visible">
itemtemplate here...
</ListBox>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>

How Can I Access to The Controls in DataTemplate in ListBox in WPF

I have a ListBox including an ItemTemplate with a StackPanel. I want to access that stackpanel and change its visibility.
(Change it's visibility to collapsed when I click mouseleftbutton "closeAll")
I can do that with FindDescendantByName Method but it works for only listbox items on screen (Only first 10 items) but when I am scrolling down, I see that this is not working for other listbox items.
I think that errors occurs because of VisualTreeHelper. What can I use instead of VisualTreeHelper?
Thanks..
XAML CODE
<ListBox x:Name="listBoxEditPast" SelectionMode="Single" Margin="0" Background="#272B34" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Grid.Row="0" BorderThickness="4,0,0,0" Margin="2,0,0,0" Height="29" Background="#2E323B" Width="1050" BorderBrush="#1373A9" MouseLeftButtonDown="Border_MouseLeftButtonDown">
<DockPanel Name="dockPanelPast" Margin="0,4,0,0">
<Image Name="imgArrow" Source="images/down-arrow.png" HorizontalAlignment="Left" Width="20" Height="18"/>
<TextBlock Text="{Binding CreateDate}" Name="txtTarih" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16"/>
<TextBlock Text="{Binding SarjNo}" Name="txtSarjNo" Foreground="#FF9CA518" HorizontalAlignment="Stretch" VerticalAlignment="Center" FontSize="16" Margin="50,0,0,0" Width="90"/>
<TextBlock Text="{Binding Adi}" Name="txtReceteAdi" Foreground="#FF26A053" VerticalAlignment="Center" FontSize="16" Margin="40,0,0,0" HorizontalAlignment="Stretch"/>
<Button Content="Detaylar" Style="{StaticResource BlueButton}" HorizontalAlignment="Right" VerticalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" DockPanel.Dock="Right"/>
</DockPanel>
</Border>
<StackPanel Grid.Row="1" Name="stackPanelDetay" Tag="{Binding ID}">
<DockPanel>
<TextBlock Text="Sipariş No" Foreground="#D9480F" VerticalAlignment="Center" />
<TextBlock Text="Parça" Foreground="#AF0FD9" VerticalAlignment="Center" Margin="50,0,0,0" Width="200" />
<TextBlock Text="Malzeme" Foreground="White" VerticalAlignment="Center" Margin="150,0,0,0" Width="90"/>
<TextBlock Text="Müşteri" Foreground="#AF0FD9" VerticalAlignment="Center" Margin="70,0,0,0" />
</DockPanel>
<DockPanel>
<TextBlock Text="{Binding ID}" Foreground="White" VerticalAlignment="Center" Width="100"/>
<TextBlock Text="{Binding ParcaKoduAdi}" Foreground="White" VerticalAlignment="Center" Margin="5,0,0,0" Width="200" />
<TextBlock Text="{Binding Malzeme}" Foreground="White" VerticalAlignment="Center" Margin="152,0,0,0" Width="90" />
<TextBlock Text="{Binding MusteriKoduAdi}" Foreground="White" VerticalAlignment="Center" Margin="70,0,0,0" />
</DockPanel>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
C# CODE
public static class FrameworkElementExtensions
{
public static FrameworkElement FindDescendantByName(this FrameworkElement element, string name)
{
if (element == null || string.IsNullOrWhiteSpace(name)) { return null; }
if (name.Equals(element.Name, StringComparison.OrdinalIgnoreCase))
{
return element;
}
var childCount = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < childCount; i++)
{
var result = (VisualTreeHelper.GetChild(element, i) as FrameworkElement).FindDescendantByName(name);
if (result != null) { return result; }
}
return null;
}
}
private void closeAll_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// StackPanel panel = LayoutHelper.FindElement(listBoxEditPast, n => n.GetType() == typeof(StackPanel)) as StackPanel;
for (int i = 0; i < listBoxEditPast.Items.Count; i++)
{
var element = listBoxEditPast.ItemContainerGenerator.ContainerFromIndex(i) as FrameworkElement;
if (element != null)
{
var sp = element.FindDescendantByName("stackPanelDetay") as StackPanel;
if (sp != null)
{
sp.Visibility = Visibility.Collapsed;
}
}
}
}
noting to do with the visualtreehelper, this is because the list is virtualized, so only the first ten items are created and then replaced by the ten next....and you loose your modifications
you must not work with the element in the data template by code
iterate through your data to set a boolean to true/false for all and then change the stack and bind the visibility to this boolean
<StackPanel Grid.Row="1" Name="stackPanelDetay" Visibility="{Binding myBoolean, Converter=BoolToVisibility}">

Telling a pivot page which page to open on via xaml.cs

I navigate to my pivote page by doing this
this.NavigationService.Navigate(new Uri("/PivotScreen.xaml", UriKind.Relative));
Is there a simple way to tell it which pageto open on it there are 5 pages on that pivot?
Many Thanks,
-Code
A simple thisPivot.SelectedIndex = 0; should do it.
Edit
I will add code to clarify.
.xaml file
<controls:Pivot Name="thisPivot" Title="MY APPLICATION">
<!--Pivot item one-->
<controls:PivotItem Header="first">
<!--Double line list with text wrapping-->
<ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PivotItem>
<!--Pivot item two-->
<controls:PivotItem Header="second">
<!--Triple line list no text wrapping-->
<ListBox x:Name="SecondListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17">
<TextBlock Text="{Binding LineOne}" TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding LineThree}" TextWrapping="NoWrap" Margin="12,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PivotItem>
</controls:Pivot>
Code behind
// Constructor
public MainPage()
{
InitializeComponent();
// Set the data context of the listbox control to the sample data
DataContext = App.ViewModel;
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
// Load data for the ViewModel Items
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
thisPivot.SelectedIndex = 1;
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}

PreviewMouseLeftButtonDown not firing

I have a ListBox, an IEnumerable is the data source. When a ListBoxItem is clicked, I want access to that object so I can grab some data and show another window.
Here is my ListBox XAML
`<ListBox Name="listBox1" Margin="0" Width="1010" Height="275" BorderThickness="0" BorderBrush="{x:Null}" Cursor="Arrow" HorizontalAlignment="Center" VerticalAlignment="Top" SelectionMode="Single" FontFamily="DIN" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Focusable="False" IsHitTestVisible="False" IsTextSearchEnabled="False" >`
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBox_MouseLeftButtonDown"></EventSetter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:Offer}">
<StackPanel Margin="0" Width="200" Height="275" Background="Black" Name="sp">
<Image Source="{Binding Image}" Width="200" Height="131" Margin="0"></Image>
<TextBlock Padding="5" Background="Black" Text="{Binding Name}" Foreground="White" FontFamily="DIN medium" FontWeight="Bold" FontSize="16" Width="200" Margin="0"></TextBlock>
<TextBlock Padding="5,0,5,0" Background="Black" Text="{Binding Date}" Foreground="White" FontFamily="DIN medium" FontWeight="Bold" FontSize="14" Width="200" Margin="0"></TextBlock>
<TextBlock Padding="5" Background="Black" Text="{Binding Description}" Foreground="White" FontFamily="DIN light" FontSize="16" Width="200" Margin="0" TextWrapping="WrapWithOverflow"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Background="Black" CanHorizontallyScroll="True" CanVerticallyScroll="False" FlowDirection="LeftToRight" Margin="0" Orientation="Horizontal" Width="1010" Height="275"></VirtualizingStackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>`$
other pertinent info
CurrentItems = (from offerCatType in offerRes.OfferCategory
where offerCatType.type == Type
from offers in offerCatType.Offer
where new DateTime(Convert.ToDateTime(offers.startDate).Year,
Convert.ToDateTime(offers.startDate).Month, 1) <= MonthYear && Convert.ToDateTime(offers.endDate) >= MonthYear
select new Offer
{
Name = offers.name,
Description = offers.description,
Date = String.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(offers.startDate)) + " to " + String.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(offers.endDate)),
ClickThruUrl = offers.ChannelInfo.refClickThroughLink,
ReferenceID = offers.ChannelInfo.refId,
Image = offers.ChannelInfo.refLink
}
);
listBox1.ItemsSource = CurrentItems;
protected void ListBox_MouseLeftButtonDown(object sender, RoutedEventArgs e)
{}
Is it possible some of my styling could blow away this event? I had it working earlier today, then was fixing a couple more styling items, then, the click code stopped working.
Set the IsHitTestVisible property to true instead of false for the listbox and you will get mouse events.

Categories

Resources