How do you use StackPanel.FindName in WPF? - c#

I have a stack panel with a set of four image buttons inside of it. When I try to search by the name of the image button it comes back null. What is wrong with my code?
XAML:
<StackPanel x:Name="swatchClearStackPanel" Orientation="Vertical" Grid.Column="1" Grid.Row="2" Margin="0,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Bottom">
<imgbutton:ImageButton x:Name="ClearSwatchColor1" Grid.Row="0" Grid.Column="2" Style="{DynamicResource ButtonClearSwatchColor}" Margin="5,8,5,5" HorizontalAlignment="Left" Visibility="Hidden" Click="ClearSwatchColor_Click" />
<imgbutton:ImageButton x:Name="ClearSwatchColor2" Grid.Row="0" Grid.Column="2" Style="{DynamicResource ButtonClearSwatchColor}" Margin="5,18,5,5" HorizontalAlignment="Left" Visibility="Hidden" Click="ClearSwatchColor_Click"/>
<imgbutton:ImageButton x:Name="ClearSwatchColor3" Grid.Row="0" Grid.Column="2" Style="{DynamicResource ButtonClearSwatchColor}" Margin="5,18,5,5" HorizontalAlignment="Left" Visibility="Hidden" Click="ClearSwatchColor_Click"/>
<imgbutton:ImageButton x:Name="ClearSwatchColor4" Grid.Row="0" Grid.Column="2" Style="{DynamicResource ButtonClearSwatchColor}" Margin="5,20,5,5" HorizontalAlignment="Left" Visibility="Hidden" Click="ClearSwatchColor_Click"/>
</StackPanel>
And the C#:
EDIT: Note: The button name is hard coded to show that it cannot find it, the end result will be a variable as its one method that several buttons will reference. This code triggers after the UI has loaded. (The user clicks a button and this code runs. Same window, same everything.)
var swatchClearButton = swatchClearStackPanel.FindName("ClearSwatchColor1");

Related

How can I create a button before a control in another parent?

I am having a XAML issue and I am sure it's because of the fact that order matters in XAML.
The question is, how can I create the button after the control with the Selected event on it if the button is in another parent? Because XAML order matters, and the Selected event has in its C# method a button which is made AFTER the control with the Selected event, and so when the application starts, the button is null, I get this this exception:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
SettingsStartupUpdateCheckBtn was null.
In XAML I have this code to make the button starting from line 782,:
<Button x:Name="SettingsStartWithWindowsBtn"
BorderThickness="10"
Grid.Row="2"
Grid.Column="4"
Width="40"
Height="40"
Content="{DynamicResource off}"
FontSize="18"
FontWeight="DemiBold"
HorizontalAlignment="Center"
Focusable="False"
IsTabStop="False"
IsEnabled="True"
VerticalAlignment="Center"
BorderBrush="White"
Foreground="Red"
Click="SettingsButtons_Click"
Style="{DynamicResource RoundedCheckboxStyle}"/>
and I have this XAML code to make a ComboBox control at line 818:
<ComboBox x:Name="SettingsLanguageSelection"
BorderThickness="10"
Grid.Column="4"
Grid.ColumnSpan="1"
Grid.Row="0"
Width="100"
Height="30"
FontSize="16"
Focusable="False"
IsTabStop="False"
HorizontalAlignment="Center"
VerticalAlignment="Center"
BorderBrush="White"
Style="{DynamicResource ComboBoxFlatStyle}">
<ComboBoxItem x:Name="SettingsLanguageEnglish"
Content="{DynamicResource english}"
FontSize="15"
Focusable="False"
IsSelected="True"
Selected="SettingsLanguageEnglish_Selected"/>
<ComboBoxItem x:Name="SettingsLanguageRomanian"
Content="{DynamicResource romanian}"
Focusable="False"
FontSize="15" Selected="SettingsLanguageRomanian_Selected"/>
</ComboBox>
The Selected events go to a C# method that references the SettingsStartWithWindowsBtn button, which of course is null because it's created after the ComboBox control.
Now I don't know how to create the button before the ComboBox control if the button is in another parent in the XAML code.

Mouse events not firing in Windows WPF

I have a simple WPF application
<Grid>
<Border BorderBrush="Black" BorderThickness="1"
HorizontalAlignment="Left" Height="388" Margin="10,10,0,0" VerticalAlignment="Top" Width="329"/>
<Image x:Name="PreviewImg" HorizontalAlignment="Left" Height="400" Margin="10,10,0,0" VerticalAlignment="Top" Width="329" MouseDown="PreviewImgMouse" AllowDrop="True" PreviewMouseWheel="PreviewImg_PreviewMouseWheel" MouseEnter="Entered" Focusable="True"></Image>
<Label Content="Matched Products" HorizontalAlignment="Left" Margin="344,10,0,0" VerticalAlignment="Top" Width="290"/>
<Label Content="Unknown Products" HorizontalAlignment="Left" Margin="661,10,0,0" VerticalAlignment="Top" Width="290"/>
<TextBlock x:Name="MatchedProducts" HorizontalAlignment="Left" Margin="349,36,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="362" Width="295"/>
<TextBlock x:Name="UnknownProducts" HorizontalAlignment="Left" Margin="666,36,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="362" Width="295"/>
<Grid HorizontalAlignment="Left" Height="50" Margin="10,410,0,0" VerticalAlignment="Top" Width="957">
<Button x:Name="FinalizeBtn" Content="Finalize" HorizontalAlignment="Left" Margin="826,10,0,0" VerticalAlignment="Top" Width="120" Height="30"/>
<Button x:Name="NewEntryBtn" Content="Create new entry" HorizontalAlignment="Left" Margin="701,10,0,0"
VerticalAlignment="Top" Width="120" Height="30"/>
<Button x:Name="FreeBtn" Content="Free Button" HorizontalAlignment="Left" Margin="576,10,0,0" VerticalAlignment="Top" Width="120" Height="30"/>
</Grid>
</Grid>
All the events defined under the Image component are not firing, I tried all of googles first page searches, but none of them work.
I read about the difference of Routed events in my application both MouseDown and PreviewMouseDown should work.
The only thing that can block the image is Border component, but I did try without it and still nothing, and since it is declared first the image should overlay it...?
All function names are correct since they were automatically generated.
I can paste in the .cs file if it can help.
Thanks
E1: Somehow it works now, but I have no idea what fixed it...
One change I did was mode the image inside the border:
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="388" Margin="10,10,0,0" VerticalAlignment="Top" Width="329">
<Image x:Name="PreviewImg" Height="400" Margin="0" VerticalAlignment="Center" HorizontalAlignment="Center" Width="329" MouseDown="PreviewImgMouse" AllowDrop="True" PreviewMouseWheel="PreviewImg_PreviewMouseWheel" MouseEnter="Entered" Focusable="True"/>
</Border>
But I can't image that this fixed it.
You need to define a source for your image like <Image Source="image.jpg". Without the image the events will not fire.
For advanced users
Use a Button styled as an Image, see #Richard's answer with WPF Image Control with Click event
For beginners
The Image component needs a source to fire events as posted by #Örvar. Adding a transparent source is the solution, although a hacky one.
One way of achieving what you want is to place the image in a button and removing the button styles. There is an excellent example here:
WPF Image Control with Click Event

How to be able to have a two line text on Button content WPF ? And be able to click on text and activate Button1_click

The code is shown below. When I try to click on the middle of the button you are actually clicking the TextBlock and not triggering the Button2_click event. Only if you click outside of the TextBlock and still inside of the Button it would trigger the event.
What is the right way of having a two line text inside the content of the Button.I'm a new on StackOverFlow so I don't have enough reputation to add a picture. Please some Help with this question. Thanks for the Help in Advance.
<Button Content="" Height="77" HorizontalAlignment="Left" Margin="70,136,0,0" Name="Button2" VerticalAlignment="Top" Width="202" />
<TextBlock TextAlignment="Center" Padding="2" Margin="80,152,0,135" FontSize="16" HorizontalAlignment="Left" Width="181" TextWrapping="NoWrap" Foreground="#FF8787DB" FontWeight="Bold"> Calculate CTR Losses<LineBreak></LineBreak> (From FTD Data)</TextBlock>
Put your TextBlock inside your button:
<Button HorizontalAlignment="Left" Margin="70,136,0,0" Name="Button2" VerticalAlignment="Top">
<TextBlock Padding="2" FontSize="16" TextWrapping="NoWrap" Foreground="#FF8787DB" FontWeight="Bold">
Calculate CTR Losses
<LineBreak />
(From FTD Data)
</TextBlock>
</Button>
This will implicitly set Button.Content, it's an equivalent of:
<Button HorizontalAlignment="Left" Margin="70,136,0,0" Name="Button2" VerticalAlignment="Top">
<Button.Content>
<TextBlock Padding="2" FontSize="16" TextWrapping="NoWrap" Foreground="#FF8787DB" FontWeight="Bold">
Calculate CTR Losses
<LineBreak />
(From FTD Data)
</TextBlock>
</Button.Content>
</Button>
Your original code overlays the TextBlock over the button, so both of these elements aren't related.

C# XAML Listbox collapse when clicked

I'm new in XAML for Windows Phone 8.1 and have some troubles with
making a Stackpanel clickable
collapse Item, when clicked
My work so far looks like that:
And the Code to that (please correct me, if there are major flaws):
<Border CornerRadius="15" Background="#FF595656" Margin="0" Grid.ColumnSpan="2" Height="80">
<StackPanel Orientation="Horizontal">
<StackPanel Width="20" HorizontalAlignment="Left" VerticalAlignment="Top" />
<StackPanel HorizontalAlignment="Left" Height="80" Margin="0,0,0,0" VerticalAlignment="Center" Width="51">
<Image HorizontalAlignment="Left" Height="51" Margin="0,15,0,0" Width="51" Source="Assets/fish.png" Stretch="Fill" RenderTransformOrigin="2.307,0.881" VerticalAlignment="Center"/>
</StackPanel>
<StackPanel Width="10" HorizontalAlignment="Left" VerticalAlignment="Top" />
<StackPanel HorizontalAlignment="Left" Height="80" Margin="0" VerticalAlignment="Top" Width="310">
<TextBlock HorizontalAlignment="Left" Height="25" Margin="0,20,0,0" TextWrapping="Wrap" Text="Entry 1" Width="310" VerticalAlignment="Top" FontSize="18" Foreground="Black" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Height="17" Margin="0" TextWrapping="Wrap" Text="Short description Entry 1" Width="310" VerticalAlignment="Top" Foreground="#FF0097FF"/>
</StackPanel>
</StackPanel>
</Border>
This code will later be wrapped inside a ListBox with Image, Entry 1 and the short description being bound:
<ListBox x:Name="ListBox1" Margin="0"
Width="400" Height="200" HorizontalAlignment="Left"
ItemsSource="{Binding}" Grid.Row="1" VerticalAlignment="Top" Grid.ColumnSpan="2" >
<ListBox.ItemTemplate>
<DataTemplate>
// the code above
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
So my question is:
How can I make a nice looking expansion/collapse of each Item in the ListBox, whenever I click on it?
Thank you very much in advance.
The real question is here is what do you want it to collapse to? There are too many possible ways to collapse some visual data item. Do you just want to change the height of the item or do you want some fancy animation that collapse some property?
If the height is what you're looking for it's pretty easy. Just attach an Tap Event on that Border of yours. On a sidenote, you probably want to edit the ItemContainerStyle to have <Setter Property="HorizontalContentAlignment" Value="Stretch"/> so the Listbox will stretch across the screen, otherwise imho it's un-useable.
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="0,1" Tap="Border_Tap">
<StackPanel>
<!--- rest of template --->
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
Then calculate the Minimum height you want to show (make sure it's actually useable, meaning... I can Tap on it again to show the whole thing without using a Mag Glass).
private void Border_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
int minHeight = 40; // change to whatever you want
Border b = sender as Border;
if (b.ActualHeight > minHeight)
{
b.Height = minHeight;
}
else
{
b.Height = double.NaN; // this will change the height back to Auto, showing everything
}
}
Code In Action
This is just a quick solution to your question. Some people on here would rather have you create a StoryBoard Animation on the Height Property of the Selected state in the VisualStateManager. If you reword or create a new question explicitly stating you want a VisualStateManager solution, I will provide you one as well. Good luck.

TabIndex property is not working in Windows Phone 8 application

I am setting the TabIndex propertry inside the Grid.
My code is `
<Grid Margin="30,12" Grid.Row="1" Grid.Column="1" Background="White">
<TextBlock Foreground="#FF858585" Grid.Row="1" Grid.Column="0" Text="{Binding Path=LocalizedResources.Password, Source={StaticResource LocalizedStrings}}"
Padding="10,8" x:Name="tb_pwd" />
<PasswordBox TabIndex="1" Password="" Background="Transparent" BorderBrush="#FF1ba1e2"
Style="{StaticResource PwdBoxStyle}" Grid.Row="1" MaxLength="15" GotFocus="txtpwd_GotFocus_1" LostFocus="txtpwd_LostFocus_1" x:Name="txtpwd">
</PasswordBox>
</Grid>
<Grid Grid.Row="2" Grid.Column="1" Margin="30,0" Background="White">
<TextBlock Foreground="#FF858585" Grid.Row="2" Grid.Column="0" Text="{Binding Path=LocalizedResources.Domain, Source={StaticResource LocalizedStrings}}"
Padding="10,8" x:Name="tb_domain" />
<TextBox TabIndex="2" Text="" Background="Transparent" BorderBrush="#FF1ba1e2"
Style="{StaticResource TxtBoxStyle}"
Grid.Row="2" GotFocus="txtdomain_GotFocus_1"
LostFocus="txtdomain_LostFocus_1"
Grid.Column="1" x:Name="txtdomain" MaxLength="15" />
</Grid>`
But in emulator when i write username and click tab it does not work.
Any suggestions?
The SIP (keyboard) on windows phone does not include a tab key and so there is not support internally for this.
The de-facto convention in apps is to advance the tab focus from code when the user presses the enter key.
There are lots of resources online that show how to do this and helper classes to make it really simple.

Categories

Resources