I am building a windows store app and due to UI problems I have to implement checkbox inside combobox.I am stucked at following problem:I want to display the checked Item in "Combobox with Checkbox" .
What I want to do :
http://blogs.microsoft.co.il/blogs/justguy/image_2827F1EB.png
<ComboBox x:Name="cb2"
DropDownOpened="cb2_DropDownOpened_1"
DropDownClosed="cb2_DropDownClosed_1"
SelectionChanged="cb2_SelectionChanged_1"
Width="310"
ItemsSource="{Binding Members}"
DisplayMemberPath="{Binding Name}"
Height="50" BorderBrush="#FF0A2562"
Tag="{Binding index}"
>
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox
Background="Black"
BorderBrush="Black"
Tag="{RelativeSource TemplatedParent}"
Content="{Binding Name}"
IsChecked="{Binding Path=IsSelected,Mode=OneWay}"
Unchecked="CheckBox_Unchecked_1"
Click="CheckBox_Click"
/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Code Behind:
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox chk = (CheckBox)sender;
string k =(string) chk.Content;
chkstr.Add(k);
}
private void CheckBox_Unchecked_1(object sender, RoutedEventArgs e)
{
CheckBox chk = (CheckBox)sender;
string k = (string)chk.Content;
chkstr.Remove(k);
// vl.selectedmembers.Remove(key);
}
In ComboBox Standard Template defined by Microsoft add a TextBlock in ContentPresenter.
Bind the Text Property of TextBlock with string.Now you can add the checked Items to string by converting the items to string explicitly.
#Patrick : ItemTemplate and ItemContainerStyle both will work.
You need to retemplate the ComboBoxItem. Specify ComboBox.ItemContainerStyle instead of ComboBox.ItemTemplate. Inside of ComboBox.ItemContainerStyle, have a setter for Template. Copy the default template and add a CheckBox. Template-bind CheckBox.IsChecked to IsSelected, or animate IsChecked to true using an object animation in the Selected visual state.
Your approach won't work since IsSelected is a property on the ComboBoxItem container, not the data context.
Related
I have build a dynamically-generated label/checkbox-List.
The label-content gets his content from a list with some application-names. The Checkbox(es) share a single CheckBox_Click-Event, that is triggered when i hit one of the checkboxes. The Problem i got now is, i can't identify which checkbox is clicked. Because i generate the controls i can't put a name through binding behind the controls. I'm not-so-experienced in creating gui's through code. Hope you guys can help me a bit.
// This part builds my label/combo-List through a FileInfo-List
Fields = new System.Collections.ObjectModel.ObservableCollection<Field>();
foreach (var app in GetPrograms())
{
Fields.Add(new Field() {Name = app.Name, Length = 100, Required = true});
}
FieldsListBox.ItemsSource = Fields;
// Here i got the Click-Event.. but i can't identify which Checkbox is clicked
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox senderChk = sender as CheckBox;
switch (senderChk.Name)
{
//case "checkBox1": // do something
//case "checkBox2": // do something
}
}
<!-- And finally the xaml-Code, note the Bindings on label and checkbox -->
<Grid>
<ListBox x:Name="FieldsListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}" VerticalAlignment="Center"></Label>
<CheckBox IsChecked="{Binding Checked}" Click="CheckBox_Click" Margin="5,0,0,0"></CheckBox>
</StackPanel>
Because i already have a list of filenames, i thought i can bind the names on the checkboxes, so i know which checkbox was clicked and can manage the right programmname.
You did this to bind the checked propierty to the comboBox.
<CheckBox IsChecked="{Binding Checked}" ... ></CheckBox>
Do the same and Bind the propierty Uid (used to identify controls in WPF) of the combobox like this:
<CheckBox ... Uid="{Binding Name}" ... ></CheckBox>
and use it in your switch.
My problem is that after I select a item in the ComboBox, the first item or "default" item of the combobox stays empty but if I click the combobox the values beneath show up are selectable etc. but I want the clicked one to show in the "default/first" place.
What I tried so far
XAML:
<ComboBox Margin="55,0,0,10" Height="20" Width="145" VerticalAlignment="Center" HorizontalAlignment="Left"
ItemsSource="{Binding TabItems, Source={StaticResource MainWindowViewModelRefactored}, Mode=TwoWay}"
SelectedItem="{Binding SelectedItem, Source={StaticResource MainWindowViewModelRefactored}, Mode=TwoWay}"
DisplayMemberPath="Header">
</ComboBox>
Property:
public TabItem SelectedItem {
get {
return _selectedItem;
}
set {
UpdateTCVCollection(value);
_selectedItem = value;
NotifyPropertyChanged("SelectedItem");
}
}
If I open up the combobox the selecteditem is highlighted, but I also want it to be shown in the "first place" when the ComboBox is closed.
You can add a method when the index has changed, then remove the item that the user selected and add it at the begining.
I've set the value of Sorted to false because that way the value you selected won't be reorganized in your ComboBox.
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e) {
RadItem selectedItem = ComboBox1.SelectedItem as RadItem;
if (selectedItem != null) {
ComboBox1.Items.Remove(selectedItem);
ComboBox1.Items.Sorted = true;
ComboBox1.Items.Sorted = false;
ComboBox1.Items.Insert(0, selectedItem);
ComboBox1.Text = selectedItem.Text;
}
}
Add the UpdateSourceTrigger to your Combobox.
UpdateSourceTrigger=PropertyChanged
Example:
<ComboBox Margin="55,0,0,10" Height="20" Width="145" VerticalAlignment="Center" HorizontalAlignment="Left"
ItemsSource="{Binding TabItems, Source={StaticResource MainWindowViewModelRefactored}, Mode=TwoWay}"
SelectedItem="{Binding SelectedItem, Source={StaticResource MainWindowViewModelRefactored}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Header">
</ComboBox>
Have a look to this MSDN link
That should help you with your problem.
Greetings
I am building an app that will return data from database based on the items that are checked on check box. I have successfully displayed data from database by binding the data. Like this:
XAML:
<ListBox x:Name="listBox1" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="52" Margin="141,264,0,0" VerticalAlignment="Top" Width="307" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="checkBox1" IsChecked="{Binding IsSelected}" Content="{Binding NacinGrejanja}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
C#:
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
Dataset1 ds= new Dataset1 ();
GrejanjeTableAdapter gta = new GrejanjeTableAdapter();
gta.Fill(ds.Grejanje);
listbox1.DataContext = ds.Grejanje;
}
But I am unable to figure out how to extract string values from checked values and then again search for those values in database. I can't access any of the properties for checkBox1 through code.
Also I would want first to check if any of the checkbox items is checked in the first place.
You can use following linq:
ds.Grejanje.Where(item => item.IsSelected == true);
It will return the list of items which are checked.
I have such a listbox like below:
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="385" Margin="21,138,0,0" VerticalAlignment="Top" Width="273" ItemsSource="{Binding Path=locationList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Name="btnDelete" Click="btnDelete_Click" Width="15" Height="15" HorizontalAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Content="x" />
<CheckBox Name="checkBox" />
<TextBlock Name="textBox" Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And what I would like to do is to set checkbox for specific item this listbox.
I am trying to do it:
private void button4_Click(object sender, RoutedEventArgs e)
{
for(int i = 0; i < listBox.Items.Count; i++)
{
listBox.Items[i].checkBox = false;
}
}
I know I am doing an error. I would like to cast it to object of item and then set an item's property (this checkbox) to false. May anyone correct me ? Thank you in advance.
edit:
Before I was trying to do it this way:
foreach (var item in listBox.SelectedItems)
{
item.
}
but all possibilities I have got are just standard methods: Equals, GetHashCode, GetType, ToString... How I may refer to checkbox ?
Moreover I will supply my question with the insight. I would like to find a specific item by text which is in line in listbox (item) and then change checkbox for this item (same row in listbox).
Second logic to be implemented is to set all rows to selected or unselected (this is what I am trying to do now).
Thank you for response.
for (int i = 0; i < listBox.Items.Count; i++)
{
var item = listBox.ItemContainerGenerator.ContainerFromItem(listBox.Items[i]) as ListBoxItem;
var template = item.ContentTemplate as DataTemplate;
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(item);
CheckBox myCheckBox = (CheckBox)template.FindName("checkBox", myContentPresenter);
myCheckBox.IsChecked = true;
}
Likewise you can find the TextBlock with (note, you named it "textBox" not "textBlock")
TextBlock myTextBlock = (TextBlock)template.FindName("textBox", myContentPresenter);
FindVisualChild can be found here FindVisualChild reference issue
I have the following view.xaml and I bind a collection(SavedTracksCollection from viewmodel) to this list box and it displays the items in UI.
<phone:PanoramaItem Name="MusicTracks" Header="Saved Tracks" >
<Grid>
<ListBox x:Name="list" ItemsSource="{Binding SavedTracksCollection}" SelectedItem="{Binding SelectedItemTrack,Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Background="Red" >
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding TrackTitle}"/>
<TextBlock Text="{Binding TrackUri}"/>
</StackPanel>
</Button>
<DataTemplate>
</ListBox.ItemTemplate>
</Grid>
</phone:PanoramaItem>
And the i have the following property defined in my viewmodel(this viewmodel is set as data context for my view) for the selecteditem binding "SelectedItemTrack".And i am binding SavedTracksCollection to the itemsource of the list.
private SavedTracksModel _SelectedItemTrack;
public SavedTracksModel SelectedItemTrack
{
get {
return _SelectedItemTrack;
}
set
{
if (value!=null)
_SelectedItemTrack = value;
//RaisePropertyChanged("SelectedItemTrack"); I dont think we need this.Let me know otherwise.
}
}
private List<SavedTracksModel> _SavedTracksCollection = new List<SavedTracksModel>();
public List<SavedTracksModel> SavedTracksCollection
{
get
{
return GetSavedTracks();
}
set
{
this._SavedTracksCollection = value;
RaisePropertyChanged("SavedTracksCollection");
}
}
But i am not able to determine how do i capture the SelectedITem event when user selectes an item from the Listbox .Currently it doesn't trigger the set method of the SelectedITemTrack .Once i capture the event with the details of selected item binding "TrackUri" i want to go to a new page where i can play the track.
any idea how to fix the issue ?
The first solution I can think of, why not just use the SelectionChanged event on ListBox?
<ListBox x:Name="list" ItemsSource="{Binding SavedTracksCollection}"
SelectedItem="{Binding SelectedItemTrack,Mode=TwoWay}"
SelectionChanged="List_OnSelectionChanged"/>
// in code behind
private void List_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
// navigate here after validating the selected item
// or raise Command in your ViewModel programatically
}