Goal:
Change the picture from picEnlarger into picXmark if there are any input data in the textbox txtSearch.
If no data input in the textbox then the picture picEnlarger should be displayed and picXmark should be hidden.
Problem:
I know how to do it in xaml but not in c# code.
I want to change the picture from/to visibility or hidden by using C# ONLY.
Please remember that I want to use less xaml code.
I also tried making the picture to be hidden/visible in the textbox txtSearch’s event method ( private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)) with this code:
picXmark.Visibility = Visibility.Visible;
picEnlarger.Visibility = Visibility.Hidden;
Unfortunately, I retrieve this message “Object reference not set to an instance of an object”.
<TextBox Name="txtSearch" Width="143.243" TextChanged="txtSearch_TextChanged" Text="Search article" PreviewMouseLeftButtonDown="txtSearch_PreviewMouseLeftButtonDown" />
<Image x:Name="picEnlarger" Height="14" Width="14" Source="/MediaStore;component/Bilder/search_enlarger2.gif" />
<Image x:Name="picXmark" Height="8" Width="8" Source="/MediaStore;component/Bilder/search_xmark.gif" />
</TextBox>
The
TextChangedevent will work fine, the problem is that it’s being raised before all your controls are initialized.Try something like this