selected value cannot be set - c#

I have a combobox which gets its Items from some scan function.
If the user select an element, in the next time, the user's chosen item should be selected (if it is present on the scan function output). The problem is that I cannot select it.
Here is the declaration of the ComboBox:
<ComboBox Grid.Column="1" Grid.Row="0" Margin="5" Name="SerialPortNames" Text="{Binding Name}" IsEditable="False"/>
and here what I have tried so far:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string portNameSetting = Settings.Default["SerialPortName"].ToString();
SerialPortNames.ItemsSource = SerialPort.GetPortNames();
foreach (string SerialPortNameItem in SerialPortNames.Items)
{
if (SerialPortNameItem == portNameSetting)
{
SerialPortNames.Text = SerialPortNameItem; // why this is not working
break;
}
}
}
by debugging this, I get the item selected in the combobox, but it seems that something override it and it is empty!

In your code you Binded the Text propery and also setting it from code behind
Remove Text="{Binding Name}" from the combobox
<ComboBox Width="200" Height="200" Grid.Column="1" Grid.Row="0" Margin="5" Name="SerialPortNames" IsEditable="False"/>

Related

How to display only certain items in the ListView?

I have a ListView in which some elements are shown
<ListView x:Name="ListaTestiCanzone" ItemClick="TestiCanzone_ItemClick" IsItemClickEnabled="True" ItemsSource="{Binding Source={StaticResource Testo}}" SelectionChanged="TestiCanzone_SelectionChanged" Margin="0,-10,-0.167,9.667">
<StackPanel Grid.Column="1" Width="300">
<TextBlock Text="{Binding Path=NomeCanzone}" Style="{StaticResource ListViewItemTextBlockStyle}" />
<TextBlock Text="{Binding Path=NomeArtista}"/>
<TextBlock Text="{Binding Path=Anno}"/>
</StackPanel>
I created some Checkbox in a HUBSection in which I would like to see only certain items in the ListView, and not all of them.
<CheckBox x:Name="Uomo" Content="Uomo" IsChecked="{x:Null}"/>
How would I go then to display only the items in the ListView with the property "Sesso = "Uomo"?
List<Testo> song = new List<Testo>
{
new Testo
{
Anno=2012,
NomeCanzone="Estate",
NomeArtista="Jovanotti",
Sesso="Uomo",
PercorsoFile= #"/Assets/Testi/Estate.txt"
},
new Testo
{
Anno=2012,
NomeCanzone="Terra degli uomini",
NomeArtista="Jovanotti",
Sesso="Uomo",
PercorsoFile="",
Instead of binding to the collection directly, go through a CollectionView object. Then set the Filter property to a predicate that evaluates using your check box and condition, as described on MSDN.
The function would be something like:
private void ShowUomoFilter(object sender, FilterEventArgs e)
{
if (checkBox1.Checked)
{
Testo item = (Testo)e.Item;
if (item.Sesso == "Uomo")
e.Accepted = true;
else
e.Accepted = false;
}
else
e.Accepted = true;
}
Since the filter is all view-related, setting this up through the code-behind is OK.

Disable the drop down menu of a WPF ComboBox

I have a ComboBox as specified below:
<ComboBox Height="31" Margin="7,7,0,0" Name="callerID" IsEditable="True" Background="LightBlue" KeyDown="callerIDbar_KeyDown" Foreground="White" FontSize="17" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalAlignment="Top" Grid.Row="0" Grid.Column="0" />
storedCalls is a collection of phone numbers that will be populated to the ComboBox.Items:
foreach (string call in storedCalls)
{
if (call != "Enter a number to dial")
callerID.Items.Add(call);
}
All this works fine. I populate the Items primary because I like the autocomplete that is driven by the values in the ComboBox's Items collections. Is there a way the XAML to disable the drop down error, and disable the drop down menu? I.e. make a simple auto complete textbox like control?
I have seen full on TextBox controls that include a bunch of code-behind and complicated markup, and this is not what I am looking to do. I just need to disable the ability of the drop down menu from showing.
You can handle the DropDownOpened event and then close it.
So in n the XAML you get:
<ComboBox x:Name="cb" DropDownOpened="cb_DropDownOpened"/>
And in Code Behind:
private void cbCategoria_DropDownOpened(object sender, EventArgs e)
{
ComboBox cb = sender as ComboBox;
cb.IsDropDownOpen = false;
}
I prefear this solution reather than set MaxDropDownHeight to 0.
Your choice.

Win8 ListView Tap does not select item

Hi so I'm doing a note taking app and Ive got it connected to my SQL Azure Database.
The items get automatically populated through MobileServiceCollectionView.
After tapping an item, a detailView should be opened to the right...
I dont know why but tapping an item does not select it, so I cant check for listView.SelectedItem in the noteListView_Tapped event.....
What is wrong with my listview?
<ListView x:Name="noteListView" Margin="20,0,0,0" Tapped="noteListView_Tapped">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="2">
<CheckBox x:Name="CheckBoxComplete" IsChecked="{Binding Complete, Mode=TwoWay}" Checked="CheckBoxComplete_Checked" Padding="3"/>
<TextBlock x:Name="TextBlockTodoItem" Text="{Binding Title}" Padding="3" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And here the Tapped event.. It crashed on TodoItem t = (TodoItem)lv.SelectedItem;
So i added the if condition and now nothing happens except for the debug prints.
private void noteListView_Tapped(object sender, TappedRoutedEventArgs e)
{
Debug.WriteLine("0");
ListView lv = (ListView)sender;
if (lv.Items.Count > 0)
{
if (lv.SelectedItems.Count > 0)
{
TodoItem t = (TodoItem)lv.SelectedItem;
Debug.WriteLine("1");
Debug.WriteLine(t.Title);
try
{
Location l = TodoItem.StringToLocation(t.LocationTaken);
Debug.WriteLine("2");
MapLayer.SetPosition(locationIcon, l);
map.SetView(l, 15.0f);
}
catch (Exception ex)
{
Debug.WriteLine("Map set position failed.: " + ex.Message);
}
}
}
}
So what do I have to change or how do I know which item has been clicked/selected?
Why not just use the SelectionChanged event on the listview?
<ListView SelectionChanged="noteListView_SelectionChanged">
Then in your code behind
private void noteListView_SelectionChanged(object sender, RoutedEventArgs e)
{
if((sender as ListView).Items.Count > 0)
{
.....
}
}
I think (though I'm not sure) that your problem is that tapping the listView, and so the tapped event, is not the same as selecting an item in the list.

list item selection-how to display a tick image on the selected item in the list box

I have a list box and i have to display a tick mark on the item selected.I tried this code
List Box
<ListBox Height="691" HorizontalAlignment="Left" Name="listBox1" Margin="-12,71,0,0" VerticalAlignment="Top" Width="480" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="0,1,0,0" BorderBrush="#FFC1BCBC" Width="490">
<Grid Height="70">
<TextBlock
Name="clients"
Margin="10,12,0,0"
Text="{Binding Name}" FontSize="24" FontWeight="SemiBold" Foreground="Black"></TextBlock>
<Image Height="30" Width="30"
HorizontalAlignment="Left"
Name="imageTick"
Stretch="Fill"
VerticalAlignment="Center"
Source="{Binding strAccountSelectedTickPath}"
Margin="380,0,0,0" Visibility="Collapsed"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
xaml.cs
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string clientId="";
if (listBox_1.SelectedIndex >= 0)
{
(Application.Current as App).obj_subnodes = newlist[listBox1.SelectedIndex];
if ((Application.Current as App).obj_subnodes.strAccountSelectedTickPath==""||(Application.Current as App).obj_subnodes.strAccountSelectedTickPath==null)
{
if ( (Application.Current as App).obj_subnodes.strAccountSelectedTickPath = "")
{
(Application.Current as App).obj_subnodes.strAccountSelectedTickPath = "/sprinklr;component/Images/IsSelected.png";}
else{ (Application.Current as App).obj_subnodes.strAccountSelectedTickPath = ""; }
initializeListBox();
}
NavigationService.Navigate(new Uri("/Home.xaml, UriKind.Relative));
}
}
private void initializeListBox()
{
listBox1.ItemsSource = "";
listBox1.ItemsSource = newlist;
}
but the problem is suppose ifi have two items in list box say item1 and item2 and at first time i selected item1 and the tick mark displayed on item1 and after that i selected item 2 and the tick marlk displayed on item2 .but the tick mark on item1 doesnot gone.I have to display one tick mark to indicate the selected item,that is i want the tick image to display on the item i clicked.Is there any solution for this.and is there any option to get controll access for the tick image inside the list box.if there is an option i can use imagetick.visibility=visibility.collapsed.but i dont find such an option.is there any solution for this please help me
The best way to achieve this is to change the style / template of the ListBoxItem so that when it is in the 'selected' state, a tick image is displayed.
You need to create a Style that sets the Template for ListBoxItem, adding your tick image within each item. For details of how to do this, see the following blog post. You can apply this style to your ListBox by setting the ItemContainerStyle.
To turn the tick mark on / off based on selection, you will need to add a VisualState to your ListBoxItem Template, which is described in this blog post.
Download the latest Silverlight Toolkit for Windows Phone (or install it via NuGet) and use the MultiselectList instead of a regular ListBox. The MultiselectList supports checkboxes designed to match the functionality of the mail client.

DataGridCheckBox initialize to all checked

I'm using .Net 3.5 with WPF and XAML.
I have a datagrid with the first column being a DataGridCheckBoxColumn.
This is inside of a window popup.
In the constructor, I sometimes want to initialize all rows to be selected, and other cases no rows to be selected.
Using data binding, I can initialize the checkbox on a row to either checked or not checked.
But I can't get the header checkbox checked along with all the checks on the rows in the case when all rows are to be checked. How can I get to the checkbox?
<toolkit:DataGridCheckBoxColumn CellStyle="{StaticResource SingleClickEditing}" Visibility="{Binding exists}" Binding="{Binding Path=toTransfer, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False">
<toolkit:DataGridCheckBoxColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Checked"/>
</StackPanel>
</DataTemplate>
</toolkit:DataGridCheckBoxColumn.HeaderTemplate>
</toolkit:DataGridCheckBoxColumn>
I'm doing something like:
public MyPopupWindow()
{
InitializeComponent();
if(checkMode.Equals("all"))
{
// Check all the items
foreach (var item in bindList)
{
item.toTransfer = true;
}
// How to check the header checkbox?
}
I'm not sure if this is the best way to do this but you can do
<CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Checked" Loaded="CheckBox_Loaded"/>
and
void CheckBox_Loaded(object sender, RoutedEventArgs e)
{
CheckBox checkBox = sender as CheckBox;
if(checkMode.Equals("all"))
{
checkBox.IsChecked = true;
}
}

Resources