I'm new to WPF and created a draggable button with an image inside - which works nicely...but I can't seem to capture the onClick of the button which contains the image?
<Window x:Class="PAD.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PAD"
mc:Ignorable="d"
Title="C2D" Height="134" Width="134" WindowStyle="None" Background="Transparent" Foreground="Transparent" BorderBrush="Transparent" BorderThickness="0" ResizeMode="NoResize" AllowsTransparency="True">
<Grid x:Name="ClickIcon" Background="Transparent" >
<Button x:Name="dialButton" HorizontalAlignment="Left" VerticalAlignment="Top" Width="130" Height="130" Foreground="Transparent" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" >
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Ellipse Fill="Transparent"/>
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Image x:Name="phoneIcon" Width="128" Height="128" Source="c:\users\chapmd1\documents\visual studio 2015\Projects\PAD\PAD\red-phone-icon_300205.png" MouseDown="image_MouseDown" />
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
private void image_MouseDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
I tried the onClick on the Button, but nothing got captured?
Not sure what you want to achieve, but in case you want to Drag the Window while the button is pressed and then afterwards do something else then you can do it like this:
private void image_MouseDown(object sender, MouseButtonEventArgs e)
{
while (e.ButtonState == MouseButtonState.Pressed)
{
this.DragMove();
Debug.WriteLine("Dragged!");
}
var image = sender as Image;
object parent = FindParent(image);
while (parent != null && parent.GetType() != typeof(Button))
{
parent = FindParent((FrameworkElement)parent);
}
var bt = parent as Button;
if(bt != null)
bt.ClickMode = ClickMode.Press;
}
private void DialButton_OnClick(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Clicked!");
}
DependencyObject FindParent(FrameworkElement ui)
{
return ui.TemplatedParent;
}
}
Your problem is probably somewhere else. This works as expected:
XAML:
<Grid x:Name="ClickIcon" Background="Transparent" >
<Button
Click="dialButton_Click"
x:Name="dialButton"
HorizontalAlignment="Left"
VerticalAlignment="Top" Width="130" Height="130"
Foreground="Transparent"
Background="Transparent"
BorderThickness="0"
BorderBrush="Transparent">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid >
<Ellipse Fill="Transparent"/>
<ContentPresenter
Content="{TemplateBinding Content}"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
<Image x:Name="phoneIcon"
Width="128" Height="128"
Source="..."
MouseDown="phoneIcon_MouseDown"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
C#:
private void phoneIcon_MouseDown(object sender, MouseButtonEventArgs e)
{
System.Diagnostics.Trace.WriteLine("phoneIcon_MouseDown");
}
private void dialButton_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Trace.WriteLine("dialButton_Click");
}
Related
I have a problem where I run my program and it gives me a STA Error on my "Test" Window. It does not have an error if I don't have a timer going from the Page I am running till the window. I'll show and Example:
public Page1()
{
InitializeComponent();
Task.Delay(2000).ContinueWith(_ =>
{
var page = new TestW();
page.Show();
}
);
}
This is from my Page1 to open up my TestW ( Test Window )
My main code looks like this:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Luk_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void Button_Click(object sender, RoutedEventArgs e) // Login
{
Main.Content = new Page1();
Framep.Visibility = Visibility.Visible;
GridS.Visibility = Visibility.Hidden;
}
}
And my XAML Code:
<Window x:Class="date_app.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:date_app"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="400"
ResizeMode="NoResize"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
AllowsTransparency="True"
Background="Transparent"
>
<Border BorderBrush="Black"
BorderThickness="1.5"
CornerRadius="10"
>
<Border.Background>
<ImageBrush x:Name="ImageBrush" ImageSource="Images\bgapp.png" Stretch="None"/>
</Border.Background>
<Grid>
<StackPanel x:Name="Framep">
<Frame x:Name="Main" />
</StackPanel>
<StackPanel x:Name="GridS">
<!--X Luk Knappen.-->
<Grid>
<Button BorderBrush="Transparent" Name="Luk" Margin="360, 10, 10, 0" Background="Transparent" Click="Luk_Click">
<Button.Content>
<Image Source="Images\Ikke-navngivet.png" Height="20" Width="35" IsHitTestVisible="False" />
</Button.Content>
</Button>
</Grid>
<Grid>
<Image Source="Images\DateL.png" Height="200"/>
</Grid>
<!--Username.-->
<Grid>
<TextBox Style="{StaticResource WatermarkTextbox}" Name="Email" Height="30" Width="300" FontSize="20" FontFamily="Comic Sans MS" Text="Hello" Margin="0 40 0 0" TextChanged="Email_TextChanged"/>
</Grid>
<!--Password.-->
<Grid>
<TextBox Style="{StaticResource WatermarkTextbox1}" Name="Pass" Height="30" Width="300" FontSize="20" FontFamily="Comic Sans MS" Text="Hello" Margin="0 35 0 0" TextChanged="Pass_TextChanged"/>
</Grid>
<!--Login Knap.-->
<Grid>
<Button Grid.Column="0" Content="Log ind" Width="80" Height="30" FontSize="20" Margin="0 100 0 0" Click="Button_Click" />
</Grid>
<!--Opret Bruger-->
<Grid>
<Button Grid.Column="0" Content="Opret Bruger" Width="140" Height="30" FontSize="20" Margin="0 20 0 0" />
</Grid>
</StackPanel>
</Grid>
</Border>
Page1 Code:
<Page x:Class="date_app.Page1"
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"
xmlns:gif="https://github.com/XamlAnimatedGif/XamlAnimatedGif"
xmlns:local="clr-namespace:date_app"
mc:Ignorable="d"
d:DesignHeight="700" d:DesignWidth="400"
Title="LoadingM">
<Border BorderBrush="Black"
BorderThickness="1.5"
CornerRadius="10"
>
<Border.Background>
<ImageBrush x:Name="ImageBrush" ImageSource="Images\bgapp.png" Stretch="None"/>
</Border.Background>
<Grid>
<TextBlock Text="LOADING. . ." FontSize="50" FontFamily="Comic Sans MS"/>
<Image gif:AnimationBehavior.SourceUri="Images\CatGif.gif" />
</Grid>
</Border>
The problem I have: If I run with the Task Delay My test Window gets an STA Error that I don't know how to fix.
If I run without the Task Delay It just opens it all up but no STA Error.
I am trying to do this:
When I press on the Login button It is supposed to open the Page1 and hide the main window for a " Loading " Screen and then after the delay to close Main window + Page1 down to Open up the New Window. Been sitting with this for a little while :) Thanks in advance!
-- Test Window Is plain. No code there.
This is the Background.
The problem is that the Action given to ContinueWith will not be exectued on the UiThread but on a free ThreadPool-Thread, so the new TestW object will be constructed and used by a non STA-Thread. But any WPF-UI-component must be constructed and used by an STA-Thread.
The solution is to replace your Task.Delay() with
Task.Delay(2000).ContinueWith(_ =>
{
Dispatcher.Invoke(() =>
{
var page = new TestW();
page.Show();
});
});
This will delegate your execution back to the UiThread.
Is there a way to have a xaml page scroll to a particular object on a button click? For example, I have three buttons at the top of my page. I would like each button to move the scrollviewer to the top, middle, and bottom section of the page. How can I make this happen? Thanks!
I try to create the Xaml and to solve the problem. This is my solution.
This is the Xaml code.
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Button HorizontalAlignment="Left" Content="Top" Click="Button_Click" ></Button>
<Button HorizontalAlignment="Center" Content="Center" Click="Button_Click_1" ></Button><Button Content="Botton" HorizontalAlignment="Right" Click="Button_Click_2" ></Button>
<ScrollViewer Grid.Row="1" Name="MyScrollViewer" MaxZoomFactor="9">
<StackPanel>
<TextBlock Name="TextBlock" Height="20"> </TextBlock>
<Rectangle Height="300" Width="100" Fill="BlanchedAlmond"></Rectangle>
<Rectangle Height="300" Width="100" Fill="Blue"></Rectangle>
<Rectangle Height="300" Width="100" Fill="BlueViolet"></Rectangle>
<Rectangle Height="300" Width="100" Fill="Chartreuse"></Rectangle>
<Rectangle Height="300" Width="100" Fill="Crimson"></Rectangle>
</StackPanel>
</ScrollViewer>
</Grid>
And this is the .cs Code
private void Button_Click(object sender, RoutedEventArgs e)
{
MyScrollViewer.ScrollToVerticalOffset(0);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var scrollableHeight = MyScrollViewer.ScrollableHeight;
var height= scrollableHeight / 2;
MyScrollViewer.ScrollToVerticalOffset(height);
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
MyScrollViewer.ScrollToVerticalOffset(MaxHeight);
}
}
I am trying to set the progress bar visibilty in the button click, where both are inside the data template. I can't set the name for the progress bar and sent the visibility, since it is in a template.
Is there any method to achieve this. Following code is what I have tried.
<DataTemplate>
<Grid>
<Button Click="Image_Download" Loaded="Button_Loaded" Tag="{Binding .}" Width="80" Height="80" >
<Button.Template>
<ControlTemplate TargetType="Button">
<Border HorizontalAlignment="Center" VerticalAlignment="Center" >
<ContentControl Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</Button.Template>
<Image Source="c_image.png" Tag="{Binding .}" />
</Button>
<ProgressBar VerticalAlignment="Bottom" IsIndeterminate="true" Visibility="Collapsed" Style="{StaticResource CustomIndeterminateProgressBar}" />
</Grid>
</DataTemplate>
You can do this
private void Image_Download(object sender, RoutedEventArgs e) {
Button button = sender as Button;
if (button != null) {
Grid grid = button.Parent as Grid;
if (grid != null) {
ProgressBar progressBar = grid.Children.OfType<ProgressBar>().FirstOrDefault();
if (progressBar != null) {
progressBar.Visibility = Visibility.Hidden;
}
}
}
}
I am trying to learn c# and WPF application. Here I am trying to redirect from one WPF page(MainWindow.xaml) to another(HandWash.xaml) on a button click event. But the following code is throwing NULLReferenceException.
This is the MainWindow.xaml file.
<Window x:Class="MyApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
d:DesignHeight="720" d:DesignWidth="1284"
Title="StartPage" WindowStartupLocation="CenterScreen" WindowStyle="None" WindowState="Maximized" Closed="Window_Closed">
<Window.Background>
<ImageBrush ImageSource="/Images/StartPage.png"></ImageBrush>
</Window.Background>
<Grid>
<Button Content="Hand Wash" Height="794" HorizontalAlignment="Left" Name="HandWash" VerticalAlignment="Top" Width="353" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="HandWash_Click"/>
<Button Content="Bathing" Height="794" HorizontalAlignment="Left" Margin="390,0,0,0" Name="Bathing" VerticalAlignment="Top" Width="301" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="Bathing_Click"/>
<Button Content="Nail-Clip" Height="794" HorizontalAlignment="Left" Margin="730,0,0,0" Name="NailClip" VerticalAlignment="Top" Width="295" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="NailClip_Click"/>
<Button Content="Teeth Brush" Height="794" HorizontalAlignment="Left" Margin="1067,0,0,0" Name="TeethBrush" VerticalAlignment="Top" Width="310" FontSize="50" Background="Transparent" BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Click="TeethBrush_Click"/>
</Grid>
</Window>
Background code for this:
private void TeethBrush_Click(object sender, RoutedEventArgs e)
{
try
{
TeethBrush teeth = new TeethBrush(myarg);
NavigationService navService = NavigationService.GetNavigationService(this);
navService.Navigate(teeth); // NULL REFERENCE EXCEPTION at this line
}
catch (NullReferenceException ex)
{
System.Windows.MessageBox.Show(ex.Message);
}
}
This is the code for TeethBrush.xaml :
<Page x:Class="MyApplication.TeethBrush"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
d:DesignHeight="720" d:DesignWidth="1284"
Title="TeethBrush">
<Grid>
</Grid>
<Page.Background>
<ImageBrush ImageSource="C:\Users\Tonmoy\Documents\Visual Studio 2010\Projects\MyKinectApp\MyKinectApp\Images\StartPage.png"></ImageBrush>
</Page.Background>
</Page>
and the background code is:
public TeethBrush(Myargs arg)
{
InitializeComponent();
//Rest of the code
}
Please help....
You need to have a frame in main window where contents of Pages will be hosted.
If you add the following namespace to MainWindow:
xmlns:local="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
you can define a frame somewhere, e.g. in your grid:
<Grid>
<local:Frame x:Name="mainFrame">
</local:Frame>
....
Then you can navigate from your event handler like so:
TeethBrush teeth = new TeethBrush(myarg);
this.mainFrame.Navigate(teeth);
<Grid x:Name="LayoutRoot">
<Button x:Name="btn_num" Width="51" Margin="318.849,158,262.15,0" Height="45" VerticalAlignment="Top" d:LayoutOverrides="HorizontalMargin">
<Grid Height="38.166" Width="44.833">
<Label x:Name="lbl_2" Content="2" Margin="4.483,-2.042,7,-1.626" FontSize="11.333"/>
<Label x:Name="lbl_1" Content="1" Margin="4.483,0,7,-19.251" FontSize="11.333" Height="41.834" VerticalAlignment="Bottom"/>
<Label x:Name="lbl_3" Content="3" Margin="0,8.083,-15,-11.751" FontSize="11.333" HorizontalAlignment="Right" Width="33.35" Foreground="Black"/>
</Grid>
</Button>
<Button x:Name="btn_a" Content="A" HorizontalAlignment="Left" Margin="225.333,158,0,0" Width="55" Foreground="Black" Height="45" VerticalAlignment="Top" Click="btn_alt_Click" />
</Grid>
It's design will be like this
public partial class button : Window
{
static int _AClick = 0;
public button()
{
this.InitializeComponent();
}
private void btn_alt_Click(object sender, RoutedEventArgs e)
{
if (_AClick == 0)
{
_AClick = 1;
Fill();
}
else
{
btn_num.Content = "";
_AClick = 0;
}
}
public void Fill()
{
btn_num.Content = "3";
}
}
The result after window loaded
If i click A button first time. The result will be like this
If I click A button second time. The result will be like this
when I click A button second time. I need the result like below. what should I do for that.
There are a lot of ways available in WPF to achieve this. One way to do this is to have two ControlTemplates (one having all three numbers and other having just one number) and then set the template of your button in code -
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<ControlTemplate
x:Key="threeNumberTemplate"
TargetType="{x:Type Button}">
<Grid Height="38.166" Width="44.833">
<Label x:Name="lbl_2" Content="2" Margin="4.483,-2.042,7,-1.626" FontSize="11.333"/>
<Label x:Name="lbl_1" Content="1" Margin="4.483,0,7,-19.251" FontSize="11.333" Height="41.834" VerticalAlignment="Bottom"/>
<Label x:Name="lbl_3" Content="3" Margin="0,8.083,-15,-11.751" FontSize="11.333" HorizontalAlignment="Right" Width="33.35" Foreground="Black"/>
</Grid>
</ControlTemplate>
<ControlTemplate
x:Key="oneNumberTemplate"
TargetType="{x:Type Button}">
<Label x:Name="lbl_3" Content="3" FontSize="11.333"/>
</ControlTemplate>
</Grid.Resources>
<Button x:Name="btn_num" Width="51" Margin="318.849,158,262.15,0" Height="45" VerticalAlignment="Top" Template="{StaticResource threeNumberTemplate}"></Button>
<Button x:Name="btn_a" Content="A" HorizontalAlignment="Left" Margin="225.333,158,0,0" Width="55" Foreground="Black" Height="45" VerticalAlignment="Top" Click="btn_alt_Click" />
</Grid>
Code behind -
private void btn_alt_Click(object sender, RoutedEventArgs e)
{
if (_AClick == 0)
{
_AClick = 1;
btn_num.Template = FindResource("oneNumberTemplate") as ControlTemplate;
}
else
{
btn_num.Template = FindResource("threeNumberTemplate") as ControlTemplate;
_AClick = 0;
}
}
Same can be achieved through triggers by making _AClick as DependecyProperty and using it's value to swap templates in triggers.
Another approach is to have two Buttons and hide/show them based on the _AClick value in code.
You can create three DataTemplate and use DataTemplateSelector class to load the corresponding data template on run time.
MSDN - DataTemplateSelector