Archive for September, 2010

Silverlight: Getting the parent ListBox SelectedItem property of a child Button

Thanks to Anthony on StackOverflow for solving this problem for me.

I had the following XAML code, having removed the styling and formatting tags:

<ListBox Name=”ManageImageList”>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Image Name=”ManageImageThumbnail” Source=”{Binding ImageName}” />
                <StackPanel Orientation=”Vertical” >
                    <TextBlock Name=”ManageImageUrl” Text=”{Binding ImageName}” />
                    <TextBlock Name=”ManageImageComment” Text=”{Binding Comment}” />
                </StackPanel>
                <Button Name=”ManageImageDelete” Click=”ManageImageDelete_Click” Content=”X” />
           </StackPanel>
        </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

The ListBox was bound to an ObservableCollection. I wanted to give focus to the parent ListBox item when the button is clicked, making it the SelectedItem of the ListBox. But I didn’t know how to do that.

Anthony provided this answer:

private void ManageImageDelete_Click(object sender, RoutedEventArgs e)
{
     ManageImageList.SelectedItem = ((Button)sender).DataContext;
}

Exactly what I needed, and it worked perfectly.

Advertisements

2 Comments

DataTable error – “This row already belongs to another table”

I was attempting to add a DataRow from an existing DataTable into a new DataTable. 

                            Dim TempResultsFile As New DataSet
                            TempResultsFile.ReadXml(“C:\MyFile.xml”)

                           For Each Row As DataRow In TempResultsFile.Tables(0).Rows
                                   ‘Snip excess code
                                    OeaaResults.Rows.Add(Row)
                            Next

At the OeaaResults.Rows.Add(Row) line, I received the message “This row already belongs to another table”. Well duh, of course it belongs to another table. What’s your point?

But that kind of brilliant arguing did not solve my problem. Instead, I found http://www.geekzilla.co.uk/View37EB5230-5B79-4D00-800C-52D7A46CFB15.htm which did solve my problem. In the article, Paul says to use the DataTable.Import method instead of DataTable.Add. So I changed

OeaaResults.Rows.Add(Row)

to

OeaaResults.ImportRow(Row)

and the error was resolved. Joy and happiness abound.

Leave a comment

Jumping to specific record in a Silverlight ComboBox

I had a ComboBox that contained a list of U.S. states. I wanted this ComboBox to have functionality that would enable a user to enter a letter or two and have the correct item highlighted. This simple change was very easy to make.

First, a bit of XAML to define my ComboBox:

<ComboBox Name=”State” KeyDown=”State_KeyDown” Height=”25″ Grid.Column=”1″ Grid.Row=”2″ />

Now for the KeyDown event.

        private void State_KeyDown(object sender, KeyEventArgs e)
        {
            foreach (string Item in State.Items)
            {
                if (Item.StartsWith(e.Key.ToString()))
                {
                    State.SelectedItem = Item;
                    break;  // Find after the first match
                }
            }
        }

If you are just interested highlighting the element based on the first character, this little snippet will work:

        string DDLValue = string.Empty;

        private void State_KeyDown(object sender, KeyEventArgs e)
        {
            DDLValue += e.Key.ToString();

            foreach (string Item in State.Items)
            {
                if (Item.StartsWith(DDLValue))
                {
                    State.SelectedItem = Item;
                    break;  // Find after the first match
                }
            }
        }

The string can be cleared out at another time. I did it when the Close button is clicked on the parent window.

The above works, but it will not work after the first time the text is entered – the value is persisted. To clear the string of previous values, so the user could type in a new state the next time they entered the window, or even if they tabbed off the ComboBox, I added a LostFocus event to the State ComboBox definition, and added this in the code behind:

private void FollowSearchState_LostFocus(object sender, RoutedEventArgs e)
{
            DDLValue = string.Empty;
}


So, the full working code is this:

XAML:

<ComboBox Name="State" KeyDown="State_KeyDown" LostFocus="State_LostFocus" />

C#:

private void FollowSearchState_KeyDown(object sender, KeyEventArgs e)
{
            if (DDLValue.Length <= 2)
                DDLValue += e.Key.ToString();
            foreach (string Item in FollowSearchState.Items)
            {
                if (Item.StartsWith(DDLValue))
                {
                    FollowSearchState.SelectedItem = Item;
                    break;  // Find after the first match
                }
            }
            FollowSearchState.Focus();
}
private void FollowSearchState_LostFocus(object sender, RoutedEventArgs e)
{
            DDLValue = string.Empty;
}

Leave a comment