WPF : How to assign click event to the form - wpf

How to assign Click event in this? I want to do something when mouse click on this window.
It's doesn't have Click properties in both Window and Canvas
<Window Loaded="Window_Loaded"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="InClassApp.UI.TextNotify"
x:Name="Window"
Title="TextNotify"
Width="400" Height="100"
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"
ShowInTaskbar="False">
<Border CornerRadius="5">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFBAFDFF" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Canvas x:Name="LayoutRoot" >
.......
</Canvas>
</Border>

You could handle the MouseLeftButtonUp event instead.

you might want to add MouseLeftButtonDown="Window_MouseLeftButtonDown" on your <Window> element.
and add following in the code-behind file.
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// do some stuff here.
}

Related

XAML from file as element

Hi i want to load my element during program start from external file.
Below is my code:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FromFile3d" x:Class="FromFile3d.MainWindow"
Title="MainWindow" Height="400" Width="800">
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.789,0.541" StartPoint="0.225,0.543">
<GradientStop Color="Black" Offset="0.849"/>
<GradientStop Color="#FF232323" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="527*"/>
<ColumnDefinition Width="265*"/>
</Grid.ColumnDefinitions>
<Button Content="Button" Grid.Column="1" HorizontalAlignment="Left" Height="26" Margin="161,28,0,0" VerticalAlignment="Top" Width="75"/>
<Viewport3D x:Name="_viewport3D" />
</Grid>
and for last cs with load method
private void LoadCore()
{
string path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "test.xaml");
if (path != null)
{
try
{
StreamReader mysr = new StreamReader(path);
_viewport3D= XamlReader.Load(mysr.BaseStream) as Viewport3D;
}
catch (Exception e)
{
MessageBox.Show(
String.Format("Unable to parse file:\r\n\r\n{0}",
e.Message), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
and file that i want to load is Viewport3d definition with my scene and model and ends with /Viewport3D ( i couldn't paste it, don't know why )
What i'm doing wrong ? because nothing happens when program starts my model is invisible :> (it's not the problem with viewport code because it work's fine before moving to external file).
_viewport3D = XamlReader.Load(mysr.BaseStream) as Viewport3D;
You can't manipulate the control tree like that. You should instead be appending elements.
First, remove the Viewport3D from your XAML.
Second, give your grid a name, say _grid.
Third, change the aforementioned line to:
_grid.Children.Add(XamlReader.Load(mysr.BaseStream) as Viewport3D);
This is the appropriate way to manipulate the control tree.

binding the background color of a gradient inside of a WPF listbox datatemplate

So I have a datatemplate and there is board in it, here is what I wan to do.
<Border Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="5">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Transparent"/>
<GradientStop
Color="{Binding Condition, Converter={StaticResourc ConditionTypeToColorConveter}}" Offset="0.541"/>
</LinearGradientBrush>
</Border.Background>
</Border>
I can see the converter being called no problem. but the color it returns isn't displayed. Now if I do this
<Border Grid.Column="0"
Grid.Row="4" Grid.ColumnSpan="5"
Background="{Binding Condition, Converter={StaticResourc ConditionTypeToColorConveter}}">
It works just fine - of course - I don't want it to be all one color I need it to be a gradient.
Anyone have any idea what is wrong with this? it's making me batty...
This can't work because in the first example you bind a Color in the second a Brush. What type your converter returns? You named it 'ToColor' but it is working as a Brush for Background.

How to apply "Uniform" stretch property to a composite path with gradients in WPF/XAML?

Folks
I am trying to draw my company's logo vectorially with XAML, in order to put it inside UI containers whenever it is convenient.
The only requisite is that the logo won't be clipped or deformed, so the "Uniform" stretch property is what I am looking for.
Sample "semi-working" code is below:
<Grid Width="160" Height="153" >
<Rectangle Clip="M 80,0 c 41.793,0 75.879,34.086 75.879,75.878 0,0.75 -0.015,1.496 -0.036,2.24 l -33.846,-57.969 -42.204,80.387 -39.421,-68.175 -35.758,34.917 c 4.294,-37.769 36.501,-67.278 75.386,-67.278 z" Margin="0">
<Rectangle.Fill>
<RadialGradientBrush GradientOrigin="44,14" Center="44,44" RadiusX="125" RadiusY="125" MappingMode="Absolute">
<GradientStop Color="#FF1C545C" Offset="0.63"/>
<GradientStop Color="#FF3BB3C3" Offset="0.23"/>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Clip="M 151,102 c -10.528,29.287 -38.601,50.33 -71.436,50.33 -36.935,0 -67.845,-26.625 -74.533,-61.662 l 32.586,-31.299 42.204,75.131 44.986,-84.096 26.193,51.596 z">
<Rectangle.Fill>
<RadialGradientBrush RadiusY="100" RadiusX="100" GradientOrigin="49,87" Center="49,87" MappingMode="Absolute">
<GradientStop Color="{DynamicResource MarinhoMiotec}" Offset="0.27"/>
<GradientStop Color="#FF003052" Offset="0.63"/>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
The problem is: when I put this grid inside another icon-sized grid, the drawing does not shrink to uniformly fit inside it.
I guess it is so because the grid where the rectangles are drawn has fixed heigth and width. I already tried to create a single discontinuous path, but then I could not put different colors on each half of the logo.
Should I chose a different container, or should I make the coordinates relative, or use a transform, or create a style, or make a brush... I am confused with so many possible lines of action...
Any help would be appreciated.
Thanks for reading
Try wrapping your logo inside a ViewBox: http://msdn.microsoft.com/en-us/library/system.windows.controls.viewbox.aspx
Here is an example:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="150" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="150" />
</Grid.RowDefinitions>
<Viewbox>
<Grid Width="160" Height="153" >
<Rectangle Clip="M 80,0 c 41.793,0 75.879,34.086 75.879,75.878 0,0.75 -0.015,1.496 -0.036,2.24 l -33.846,-57.969 -42.204,80.387 -39.421,-68.175 -35.758,34.917 c 4.294,-37.769 36.501,-67.278 75.386,-67.278 z" Margin="0">
<Rectangle.Fill>
<RadialGradientBrush GradientOrigin="44,14" Center="44,44" RadiusX="125" RadiusY="125" MappingMode="Absolute">
<GradientStop Color="#FF1C545C" Offset="0.63"/>
<GradientStop Color="#FF3BB3C3" Offset="0.23"/>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle Clip="M 151,102 c -10.528,29.287 -38.601,50.33 -71.436,50.33 -36.935,0 -67.845,-26.625 -74.533,-61.662 l 32.586,-31.299 42.204,75.131 44.986,-84.096 26.193,51.596 z">
<Rectangle.Fill>
<RadialGradientBrush RadiusY="100" RadiusX="100" GradientOrigin="49,87" Center="49,87" MappingMode="Absolute">
<GradientStop Color="{DynamicResource MarinhoMiotec}" Offset="0.27"/>
<GradientStop Color="#FF003052" Offset="0.63"/>
</RadialGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Viewbox>
</Grid>
This will render the logo in a 50x50 grid cell.

Strange behavior on ShowDialog in WPF

I create separate Window, design it with XAML and when I invoke ShowDialog from main form it seems like my dialog (Window) blinks once and then shows itself. Is it a common behavior? I didn't notice that in while working with Windows Forms. I also ran application on another computer, and get the same thing. It bothers me, cause I was developing a simple game, and it's not the effect I would like users to experience.
It is not a complicated dialog, considering the design. It contains just label and button. Here is one sample:
<Window x:Class="A_Boggle.Info"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Info" Height="300" Width="670" AllowsTransparency="True" WindowStyle="None" Background="Transparent" BorderBrush="Transparent" Foreground="Transparent" ShowInTaskbar="False" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Closing="Window_Closing">
<Grid>
<Border Background="Transparent" Visibility="{Binding Visibility}">
<Border BorderBrush="#FF7C4400" BorderThickness="4"
CornerRadius="10,0,10,0" VerticalAlignment="Center" HorizontalAlignment="Center" Height="177.5" Width="596.25">
<Border.Background>
<RadialGradientBrush Center="0.5,0.5" GradientOrigin="0.5,0.5" RadiusX="0.479" RadiusY="0.524">
<GradientStop Color="#FFF58611" Offset="0"/>
<GradientStop Color="#FFF58611" Offset="0.11798000335693359"/>
<GradientStop Color="#FFE9B231" Offset="1"/>
</RadialGradientBrush>
</Border.Background>
<Border.BitmapEffect>
<DropShadowBitmapEffect Color="Black" Opacity="0.5" Direction="270" ShadowDepth="0.7" />
</Border.BitmapEffect>
<Grid>
<Separator Height="20" Name="separator1" Margin="8.75,0,6.25,45" VerticalAlignment="Bottom" />
<Button Style="{DynamicResource OrangeButton}" Margin="406.25,0,6.25,6" Height="37.75" VerticalAlignment="Bottom" FontSize="16" Name="dialogButton" Click="dialogButton_Click"></Button>
<Label FontFamily="Resources/#French Grotesque" FontSize="20" Foreground="#FF7C4400" Margin="8.75,20,6.25,71.25" Name="messageLabel"></Label>
</Grid>
</Border>
</Border>
</Grid>
No. Blinking on ShowDialog is not a common behaviour. Could you first try with an empty Window:
new Window().ShowDialog();
in order to see if the problem persists?
Aside from the main topic, WPF/XAML might be not the proper technology for a complicated game due to performance reasons (although for a simple one must be OK).

How to position UserControl in the parent canvas

I want to place this UserControl at Canvas.Left="168", Canvas.Top="213".
However, the control appears at a corner. What should I do?
If I put the values at the point of usage for this class, the values are returned as NaN
In that case how can I get the correct Left and Top Values?
Usage:
<Canvas x:Name="DesignerCanvas"
ClipToBounds="True"
SnapsToDevicePixels="True">
<Gr:BareNode />
</Canvas>
UserControl:
<UserControl x:Class="DiagramDesigner.BareNode"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ContentControl Width="50"
Height="50"
Padding="2"
Canvas.Left="168" Canvas.Top="213">
<Ellipse IsHitTestVisible="False" >
<Shape.Fill>
<RadialGradientBrush Center="0.2, 0.2" GradientOrigin="0.2, 0.2" RadiusX="0.8" RadiusY="0.8">
<GradientStop Color="LightBlue" Offset="0"/>
<GradientStop Color="Blue" Offset="0.9"/>
</RadialGradientBrush>
</Shape.Fill>
</Ellipse>
</ContentControl>
</Grid>
</UserControl>
I'm not sure if you tried this or not, but just from looking at the XAML it appears that you are trying to set the position of the user control inside the user control. That won't work. You need to put it where you use the user control
<Canvas x:Name="DesignerCanvas"
ClipToBounds="True"
SnapsToDevicePixels="True">
<Gr:BareNode Canvas.Left="168" Canvas.Top="213"/>
</Canvas>
Take the Canvas.Left="168" Canvas.Top="213" part out of the ContentControl declaration of inside the user control.

Resources