+ 2
Help with WPF - Rowdefination MouseDown Event [solved]
I wanted to ask if someone might know how to use the MouseDown Event for a single row. Case: I made a application which have two rows the first row's the header with title minimize and close Button and the second's the Rest. Now I want to make the application draggable when I press MouseDown. Sry I can't send the code for that because it's WPF.
17 Antworten
+ 2
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0"
Orientation="Horizontal"
MouseDown="HeaderMouseDown">
<Label Content="HeaderText" />
<!-- whatever you like in the header -->
</StackPanel>
<Border Grid.Row="1">
<!-- your content -->
</Border>
</Grid>
The Border can be anything you want and isn't just added to show the syntax. You could also make it another grid or some kind of panel. Depends on the content you want to add. The Stackpanel can also be something different.
For documentation I'd take a look at docs.microsoft.net or just google "wpf controls".
For learning the wpf basics you could watch tutorials on YouTube (google "Tim Corey wpf").
+ 1
Xaml:
Add the Mouse down event to the element holding your header.
<StackPanel MouseDown="HeaderMouseDown" >
</StackPanel>
Code-Behind:
implement HeaderMouseDown
private HeaderMouseDown(object sender, MouseButtonEventArgs e)
{
if(e.ChangedButton == MouseButton.Left)
{
this.DragMove();
}
}
+ 1
So I got the Problem now and Alex sry, I found out that I already made a <Grid> element to define the 0 Row for the header (Because I Made columns for title, close, min Button) so I just needed to add MouseDown to that element.
edit: Tho I now have the Problem that not the entire header have the MouseDown Event and I can just drag with minimal area.
+ 1
I didn't realize you asked a follow up question, sorry.
The MouseDown event is implemented in the UIElement class, which is a base class for most elements you can use (or maybe all? I would need to look it up myself ^^).
This means you can use it on all kinds of elements. Even directly on the window itself. Or, in your case, on the nested grid you are using for the header.
+ 1
It should be draggable on the whole row, if you attach it to the inner grid
<Grid>
<!-- row definitions -->
<Grid Grid.Row="0" MouseDown="xxx">
<!-- column definitions for header -->
<!-- header content -->
</Grid>
<!-- main content with row set to 1 -->
</Grid>
+ 1
Where are your column definitions? If they are set for the outer grid then that's the problem. If that's the case either set Grid.ColumnSpan="2" in the inner grid or move the column definitions inside the inner grid.
<Grid>
<!--Row Definitions-->
<Grid Grid.Row="0">
<!--Column definitions -->
<!-- Header -->
</Grid>
</Grid>
or
<Grid>
<!--Row Definitions-->
<!--Column definitions -->
<Grid Grid.Row="0"
Grid.ColumnSpan="2">
<!-- Header -->
</Grid>
<!-- content with row="1" + col span 2 -->
</Grid>
I'd prefer the column definition inside the inner grid though.
+ 1
I wrote the Code Part down (HTML else it would have been less readable)
https://code.sololearn.com/WWO5bCg5O16f/?ref=app
+ 1
Alex Okay it's solved now... Actually quite easy just add a transparent Background or... Just a Background. sry for stealing your time and thanks for the Help
0
Alex does that mean when I made:
<Grid>
<Grid.RowDefination>
<Rowdefination/>
<Rowdefination/>
</Grid.RowDefination>
</Grid>
That I add <StackPanel> Like:
<RowDefination>
<StackPanel MouseDown=""/>
</RowDefination>
Or
<Grid>
<StackPanel MouseDown=""
Grid.Row=""/>
</Grid>
Sorry when I wrote it a bit wrong but I think it should be understable what I meant.
0
Is there any place where all the Basic Blocks for WPF are explained.
0
Alex Okay thanks. I just make a Login Windows with a Tutorial as start Up. Border and such do I already have but he Made the entire Window draggable where ever you want.
0
No problem Alex but I think I should also look it up a bit more.
Currently I face the trouble that I set an <TextBlock> element inside the <Grid> element and MouseDown just go with that.
MouseDown ignore all height or width values I give to grid or something else.
It always take the area of TextBlock, for example the Window's 500: height and 800: width, the header's 50: height and *:width, but now does the MouseDown Take the area of TextBlock also 50: height and 400: width(Header column's so big).
It's annoying because the TextBlock doesn't even Take all and I played there with margin(+I wanted to later Set Horizont allignment Center)
edit: Sry I make it short. The TextBlock in the First 0 column set the area for MouseDown for some reasons.
0
You don't want it to be draggable on the whole header row? Or what's the question?
0
Alex I want it to be draggable with the entire header Row. But it's only draggable in the area of <Textblock>.
0
Sadly that was what I had the entire time.
<Border>
<Border.Background>
<!--image-->
</Border.Background>
<Border>
<!--Background with liniar Gradient-->
<Grid>
<!--Row Definition-->
<Grid Grid.Row="0" MouseDown>
<TextBlock/>
<Button/>
<Button/>
</Grid>
</Grid>
</Border>
</Border>
0
That's the construct in general. I really don't know from where that Trouble come, and maybe I should first make more of the Window functions.
I can also just leave the Text centered for now then I have most of the Header.
0
Alex The columns Definition's already in the <Grid Grid.Row="0" MouseDown="">. Also in the inner part.
edit: I also tried making more columns also
<ColumnDefination/> //Freespace
<ColumnDefination width="Auto"/> //Text
<ColumnDefination/> //Freespace
<ColumnDefination width="25"/> //Button
<ColumnDefination width="25"/> //Button
<ColumnDefination width="5"/> //Freespace