WPF Button click has no references - c#

I have a button inside a Grid container that is inside a ScrollViewer Container.
When I double click the button to auto fill the button click code in c# I get no references.
<ScrollViewer x:Name="_scroll">
<Grid Height="584">
<Button x:Name="n_one" Content="N" HorizontalAlignment="Left" Margin="134,165,0,0" VerticalAlignment="Top" Width="30" FontSize="18" FontWeight="Bold" Height="23"/>
</Grid>
</ScrollViewer>
There is more stuff in the Grid Container but they are just labels and buttons. They other elements in the container aren't suffering from the same issue.
private void n_one_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("Working")
}
I do have the Console setup and working shown with previous code. Visual Studio is telling me i have 0 references to n_one_Click why is this?
If this changes anything, I did copy this button a few times, I did rename these buttons, but they also have the same problem, elements that aren't related but still in the same container aren't affected.

If you really want the n_one button to fire your n_one_Click method, I would expect your XAML to look like this:
<ScrollViewer x:Name="_scroll">
<Grid Height="584">
<Button
x:Name="n_one"
Content="N"
Click="n_one_Click" <========== Here
HorizontalAlignment="Left"
Margin="134,165,0,0"
VerticalAlignment="Top"
Width="30"
FontSize="18"
FontWeight="Bold"
Height="23"/>
</Grid>
</ScrollViewer>

Related

Wpf send layout object not by button or textbox

I've got a little bit problem with sending my layout object. I'm trying to create an WPF app in style like a chrome with draggable cards. To do this, I'm using OpenControls.Wpf library + Prism + MahApps.Metro
Layout declarated in main xaml view
<dockManager:LayoutManager x:Name="_layoutManager" Grid.Row="1" Grid.Column="0"
DocumentsSource="{Binding Documents}" ToolsSource="{Binding Tools}"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch" UseLayoutRounding="False"
Margin="0,-4,0,0">
<dockManager:LayoutManager.Theme>
<themes:ModernTheme/>
</dockManager:LayoutManager.Theme>
<dockManager:LayoutManager.DocumentTemplates>
<DataTemplate DataType="{x:Type usViewModel:UsersModule_ViewModel}" >
<usViews:UsersModule_View x:Name="UsersModuleModule" Margin="4"/>
</DataTemplate>
</dockManager:LayoutManager.DocumentTemplates>
</dockManager:LayoutManager>
Buttons to open projects in card in the same xaml file
<StackPanel>
<Button x:Name="myButton" Style="{StaticResource ButtonMore}" Content="Users"
Command="{Binding LoadModule1Command}"
CommandParameter="{Binding ElementName=_layoutManager}"
HorizontalAlignment="Left" Margin="24,80,24,0" VerticalAlignment="Top" Width="208" Height="40"/>
</StackPanel>
By the DelegateCommand { get; private set; } in my module, through the button CommandParameter is sending an layoutManager object. I need to get that object, but without click any button or textbox.
So my main question:
Is it possible, to send an object without any buttons or is it possible to programmatically call of the click event in WPF.
I've search something about that problem, but did not find any solution.

How to use click event in MaterialDesign:PackIcon in WPF?

I used materialDesign:PackIkon in my WPF application.
this is my code at xaml for the PackIcon
<ListViewItem Background="White" Height="55" >
<StackPanel Orientation="Horizontal">
<materialDesign:PackIcon Kind="CardMembership" MouseDown="PackIconMember_MouseDown" Height="40" Width="25" Foreground="#FF0959A8" />
<Button x:Name="btnMember" Click="btnMember_Click" Content="Member" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="Black" FontSize="10" />
</StackPanel>
</ListViewItem>
I have a few PackIcon in my xaml. each of them in different listview.
what I want is when user click on the Icon it will process the event in PackIconMember_MouseDown
There is no error in my code above, the problem is sometimes the code work. I means when user click on the icon it will process the event. but sometimes user need to click multiple time for it to process the event. I don't know why this happen.
Any idea on what I should do with this ? or any suggestion to replace the MouseDown event.
its only work when I click on icon with the blue color.
it does not working when I click on the white space that I show with the arrow. how can I do to make it work when user click anywhere on the icon ? is it possible ?
if I do inside button, the packIcon does not appear
<Button Background="{x:Null}" BorderBrush="{x:Null}" Foreground="Black" Height="20" Width="25" Margin="10">
<materialDesign:PackIcon MouseDown="PackIconMember_MouseDown" TouchDown="PackIconMember_MouseDown" Kind="CardMembership" />
</Button>
Set the Background property of the PackIcon to Transparent:
<materialDesign:PackIcon Kind="CardMembership" MouseDown="PackIconMember_MouseDown" Height="40" Width="25" Foreground="#FF0959A8"
Background="Transparent" />
This should capture the clicks also on the "empty" parts of the icon.
Make The Icon as Part of the Button Like:
<Button x:Name="btnMember" Click="btnMember_Click" Content="Member" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="Black" FontSize="10" >
<materialDesign:PackIcon Kind="CardMembership" Height="40" Width="25" Foreground="#FF0959A8" />
</Button
But you have to set the margin

Cannot close silverlight popup with custom control in it

I have a silverlight app with two textboxes. When I focus on one of them, I want my popup with my "virtual" keyboard to show up (works). But, the problem is when I want to close the popup. I want to be able to close it with my "X" button (red color on the screen). How can I do it? I tried everything - delegates, INotifyProperyChanged. Nothing seems to work. In my XAML code you can see that I'm not able to access any of the buttons from MainPage - only from the code of the Custom Control. Here's the problem.
<Grid x:Name="LayoutRoot" Background="#DB1E1E1E" Margin="0,0,-89,-114">
<TextBox x:Name="txt1"
HorizontalAlignment="Left" Height="23"
Margin="10,10,0,0" TextWrapping="Wrap"
Text="TextBox" VerticalAlignment="Top"
Width="398" GotFocus="txt1_GotFocus"/>
<TextBox
HorizontalAlignment="Left"
Height="23"
Margin="10,82,0,0"
TextWrapping="Wrap"
Text="TextBox"
VerticalAlignment="Top"
Width="398"/>
<Popup x:Name="popup" IsOpen="True" AllowDrop="True">
<Grid x:Name="theBack" Background="Black" Margin="80,196,114,24">
<Keyboard:KeyboardControl x:Name="keyboard" Margin="0,-10,0,10"/>
</Grid>
</Popup>
<Button x:Name="btn"
Content="Button"
HorizontalAlignment="Left"
Margin="508,114,0,0"
VerticalAlignment="Top"
Width="75"
Click="btn_Click"/>
</Grid>
Without Bindings:
In KeyboardControl.xaml.cs define a custom handler for your control and a method for the click on the close button:
public event RoutedEventHandler CloseClick;
private void ButtonX_Click(object sender, RoutedEventArgs e)
{
if (CloseClick != null)
CloseClick(sender, e);
}
Then in KeyboardControl.xaml assign the "Click" event of the "X" button with the method previously created
<Button Content="X" Click="ButtonX_Click"/>
Then in your MainPage change the layout of KeyboardControl for this:
Finally in MainPage.xaml.cs:
private void keyboard_CloseClick(object sender, RoutedEventArgs e)
{
popup.IsOpen = false;
}

How to open Popup by clicking on grid view item in windows store app?

I want to open popup by clicking on button in gridView element, inside popup I have three options by clicking on that option I want to navigate to another page with the id of the element
i want to open popup on click on button inside the gridView element my code looks like :-
<GridView x:Name="Test" Grid.Row="1" Margin="20,20,20,20" >
<GridView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Left" Width="250" Height="250">
<Button Background="Red" HorizontalAlignment="Left" VerticalAlignment="Top" Height="100" Content="OpenPopup" Click="Button_PointerPressed"></Button>
<Popup x:Name="Mypopup">
<TextBlock Text="hi"/>
</Popup>
<StackPanel VerticalAlignment="Bottom" Background="{ThemeResource ListViewItemOverlayBackgroundThemeBrush}">
<TextBlock Text="{Binding Title}" Foreground="{ThemeResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextBlockStyle}" Height="60" Margin="15,0,15,0"/>
<TextBlock Text="{Binding Subtitle}" Foreground="{ThemeResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
on click event
private void Button_PointerPressed(object sender, RoutedEventArgs e)
{
Mypopup.isopen = true;
}
Error:-The name 'Mypopup' does not exist in the current context
I am a newbie.. so help me
All you have to do is on the Onclick event of your gridview's button do the following code :
popup.isopen = true
Then in your popup if you want to navigate to another page all you have to do is use Navigate on the proper event
Frame.Navigate(typeof(YourPage),YourID);
EDIT : If the buttons in your gridview are added through code and you want to do the event on those then make sure that they have different names such as : GrdButton1/GrdButton2 and then give them an event dynamicly
If they are added in the XAML then simply add it an event there
<Button x:Name="ButtonTest"
Click="ButtonTest_Click"/>
If none of this answers your question please add more details to it

How to add controls dynamically to a UserControl through user's XAML?

I want to create a user control that contains a TextBlock and a StackPanel that will allow the user to add his/her own controls to the user control dynamically in XAML.
Here is the sample XAML for my UserControl:
<UserControl x:Class="A1UserControlLibrary.UserControlStackPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="300">
<StackPanel>
<TextBlock Text="I want the user to be able to add any number of controls to the StackPanel below this TextBlock."
FontFamily="Arial" FontSize="12" FontWeight="DemiBold" Margin="5,10,5,10" TextWrapping="Wrap"/>
<StackPanel>
<!-- I want the user to be able to add any number of controls here -->
</StackPanel>
</StackPanel>
</UserControl>
I would like the user to be able to embed this user control in their XAML and add their own controls to the stack panel of the user control:
<uc:A1UserControl_StackPanel x:Name="MyUserControl_Test" Margin="10" Height="100">
<Button Name="MyButton1" Content="Click" Height="30" Width="50"/>
<Button Name="MyButton2" Content="Click" Height="30" Width="50"/>
<Button Name="MyButton3" Content="Click" Height="30" Width="50"/>
</uc:A1UserControl_StackPanel>
Doing this using the above XAML does not work. Any ideas?
You can do that, although not quite like your example. You need two things. The first is to declare a DependencyProperty of type UIElement, of which all controls extend:
public static DependencyProperty InnerContentProperty = DependencyProperty.Register("InnerContent", typeof(UIElement), typeof(YourControl));
public UIElement InnerContent
{
get { return (UIElement)GetValue(InnerContentProperty); }
set { SetValue(InnerContentProperty, value); }
}
The second is to declare a ContentControl in the XAML where you want the content to appear:
<StackPanel>
<TextBlock Text="I want the user to be able to add any number of controls to the StackPanel below this TextBlock."
FontFamily="Arial" FontSize="12" FontWeight="DemiBold" Margin="5,10,5,10" TextWrapping="Wrap"/>
<StackPanel>
<ContentControl Content="{Binding InnerContent, RelativeSource={RelativeSource AncestorType={x:Type YourXmlNamspacePrefix:ContentView}}}" />
</StackPanel>
</StackPanel>
In my opinion, if you use StackPanels, you could find that your content does not get displayed correctly... I'd advise you to use Grids for layout purposes for all but the simplest layout tasks.
Now the one difference to your example is in how you would use your control. The InnerContent property is of type UIElement, which means that it can hold one UIElement. This means that you need to use a container element to display more than one item, but it has the same end result:
<YourXmlNamspacePrefix:YourControl>
<YourXmlNamspacePrefix:YourControl.InnerContent>
<StackPanel x:Name="MyUserControl_Test" Margin="10" Height="100">
<Button Content="Click" Height="30" Width="50"/>
<Button Content="Click" Height="30" Width="50"/>
<Button Content="Click" Height="30" Width="50"/>
</StackPanel>
</YourXmlNamspacePrefix:YourControl.InnerContent>
</YourXmlNamspacePrefix:YourControl>
And the result:
UPDATE >>>
For the record, I know exactly what you want to do. You, it seems, do not understand what I am saying, so I'll try to explain it one last time for you. Add a Button with the Tag property set as I've already shown you:
<Button Tag="MyButton1" Content="Click" Click="ButtonClick" />
Now add a Click handler:
private void ButtonClick(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
if (button.Tag = "MyButton1") DoSomething();
}
That's all there is to it.

Categories