WPF Button event not firing while Grid have PreviewMouseMove set
I have button on the Grid that does not detects PreviewMouseLeftDown click event.
After some testing I figured that the problem is in <Grid PreviewMouseMove="onMouseMove" >
If I remove PreviewMouseMove="onMouseMove" part, then MouseDown event is detected, but i need that line of code, since I also have to detect mouse position inside that grid only.
XAML:
<Grid PreviewMouseMove="onMouseMove" Background="Transparent">
<ItemsControl Name="btnTableImageList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Content}"
Height="{Binding Height}"
Width="{Binding Width}"
Tag="{Binding Tag}"
Margin="{Binding Margin}"
Background="{Binding Background}"
HorizontalAlignment="Center"
PreviewMouseLeftButtonDown ="tblButton_MouseDown"
PreviewMouseLeftButtonUp ="tblButton_MouseUp"
Click="ClickHandlerTableBtn"
TextBlock.TextAlignment="Center" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Any idea is welcomed. Thanks!
c# wpf
|
show 6 more comments
I have button on the Grid that does not detects PreviewMouseLeftDown click event.
After some testing I figured that the problem is in <Grid PreviewMouseMove="onMouseMove" >
If I remove PreviewMouseMove="onMouseMove" part, then MouseDown event is detected, but i need that line of code, since I also have to detect mouse position inside that grid only.
XAML:
<Grid PreviewMouseMove="onMouseMove" Background="Transparent">
<ItemsControl Name="btnTableImageList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Content}"
Height="{Binding Height}"
Width="{Binding Width}"
Tag="{Binding Tag}"
Margin="{Binding Margin}"
Background="{Binding Background}"
HorizontalAlignment="Center"
PreviewMouseLeftButtonDown ="tblButton_MouseDown"
PreviewMouseLeftButtonUp ="tblButton_MouseUp"
Click="ClickHandlerTableBtn"
TextBlock.TextAlignment="Center" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Any idea is welcomed. Thanks!
c# wpf
1
Even if you get an answer to this, I'm afraid you are going down the rabbit hole! You should learn about Commands and maybe refine your understanding about MVVM in general. If you feel you don't need that for your project, WPF might not be the right choice. The Bindings look very suspicious to me, especially the Binding of Tag to Tag...
– themightylc
Nov 22 '18 at 6:44
@themightylc Can you please explain what is wrong with "Binding Tag"? I am using it for some data that is loaded from database, and since Buttons are generated during runtime, this is the way I am assigning data to certain button properties.
– Bodul
Nov 22 '18 at 6:52
There is nothing "wrong" with it. It is impossible to explain the concept of MVVM in a few words. But try to answer this yourself: What is the Binding Source and if it's an class you designed yourself, then why did you call the source Property "Tag"?
– themightylc
Nov 22 '18 at 7:22
I am guessing you are trying to achieve something like a gallery, maybe with pictures where you can move the pictures with your mouse but also something happens when you click on them, correct? Why would you need to Handle the Click Event, as well as MouseUp and MouseDown? Why does your Button have to be a Button?
– themightylc
Nov 22 '18 at 7:24
1
to clarify: it's awesome you're trying to make something and WPF is a great framework. I love it. But I can spot one key problem in your second line of XAML and that is, your ItemsControl does not have an ItemsSource. Which means you must fill it programatically. Which means you create the "Content" Objects programatically. Which leads to the question why the Data in "Tag" is not in the Content already. I recommend you take a few hours and watch a few Videos on MVVM, especially the MVVM light toolkit. mvvmlight.net It can't be done in five minutes but it is well worth your time.
– themightylc
Nov 22 '18 at 7:37
|
show 6 more comments
I have button on the Grid that does not detects PreviewMouseLeftDown click event.
After some testing I figured that the problem is in <Grid PreviewMouseMove="onMouseMove" >
If I remove PreviewMouseMove="onMouseMove" part, then MouseDown event is detected, but i need that line of code, since I also have to detect mouse position inside that grid only.
XAML:
<Grid PreviewMouseMove="onMouseMove" Background="Transparent">
<ItemsControl Name="btnTableImageList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Content}"
Height="{Binding Height}"
Width="{Binding Width}"
Tag="{Binding Tag}"
Margin="{Binding Margin}"
Background="{Binding Background}"
HorizontalAlignment="Center"
PreviewMouseLeftButtonDown ="tblButton_MouseDown"
PreviewMouseLeftButtonUp ="tblButton_MouseUp"
Click="ClickHandlerTableBtn"
TextBlock.TextAlignment="Center" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Any idea is welcomed. Thanks!
c# wpf
I have button on the Grid that does not detects PreviewMouseLeftDown click event.
After some testing I figured that the problem is in <Grid PreviewMouseMove="onMouseMove" >
If I remove PreviewMouseMove="onMouseMove" part, then MouseDown event is detected, but i need that line of code, since I also have to detect mouse position inside that grid only.
XAML:
<Grid PreviewMouseMove="onMouseMove" Background="Transparent">
<ItemsControl Name="btnTableImageList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Content}"
Height="{Binding Height}"
Width="{Binding Width}"
Tag="{Binding Tag}"
Margin="{Binding Margin}"
Background="{Binding Background}"
HorizontalAlignment="Center"
PreviewMouseLeftButtonDown ="tblButton_MouseDown"
PreviewMouseLeftButtonUp ="tblButton_MouseUp"
Click="ClickHandlerTableBtn"
TextBlock.TextAlignment="Center" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Any idea is welcomed. Thanks!
c# wpf
c# wpf
edited Nov 22 '18 at 5:45
Julian Stark
1,2641326
1,2641326
asked Nov 22 '18 at 5:43
BodulBodul
918
918
1
Even if you get an answer to this, I'm afraid you are going down the rabbit hole! You should learn about Commands and maybe refine your understanding about MVVM in general. If you feel you don't need that for your project, WPF might not be the right choice. The Bindings look very suspicious to me, especially the Binding of Tag to Tag...
– themightylc
Nov 22 '18 at 6:44
@themightylc Can you please explain what is wrong with "Binding Tag"? I am using it for some data that is loaded from database, and since Buttons are generated during runtime, this is the way I am assigning data to certain button properties.
– Bodul
Nov 22 '18 at 6:52
There is nothing "wrong" with it. It is impossible to explain the concept of MVVM in a few words. But try to answer this yourself: What is the Binding Source and if it's an class you designed yourself, then why did you call the source Property "Tag"?
– themightylc
Nov 22 '18 at 7:22
I am guessing you are trying to achieve something like a gallery, maybe with pictures where you can move the pictures with your mouse but also something happens when you click on them, correct? Why would you need to Handle the Click Event, as well as MouseUp and MouseDown? Why does your Button have to be a Button?
– themightylc
Nov 22 '18 at 7:24
1
to clarify: it's awesome you're trying to make something and WPF is a great framework. I love it. But I can spot one key problem in your second line of XAML and that is, your ItemsControl does not have an ItemsSource. Which means you must fill it programatically. Which means you create the "Content" Objects programatically. Which leads to the question why the Data in "Tag" is not in the Content already. I recommend you take a few hours and watch a few Videos on MVVM, especially the MVVM light toolkit. mvvmlight.net It can't be done in five minutes but it is well worth your time.
– themightylc
Nov 22 '18 at 7:37
|
show 6 more comments
1
Even if you get an answer to this, I'm afraid you are going down the rabbit hole! You should learn about Commands and maybe refine your understanding about MVVM in general. If you feel you don't need that for your project, WPF might not be the right choice. The Bindings look very suspicious to me, especially the Binding of Tag to Tag...
– themightylc
Nov 22 '18 at 6:44
@themightylc Can you please explain what is wrong with "Binding Tag"? I am using it for some data that is loaded from database, and since Buttons are generated during runtime, this is the way I am assigning data to certain button properties.
– Bodul
Nov 22 '18 at 6:52
There is nothing "wrong" with it. It is impossible to explain the concept of MVVM in a few words. But try to answer this yourself: What is the Binding Source and if it's an class you designed yourself, then why did you call the source Property "Tag"?
– themightylc
Nov 22 '18 at 7:22
I am guessing you are trying to achieve something like a gallery, maybe with pictures where you can move the pictures with your mouse but also something happens when you click on them, correct? Why would you need to Handle the Click Event, as well as MouseUp and MouseDown? Why does your Button have to be a Button?
– themightylc
Nov 22 '18 at 7:24
1
to clarify: it's awesome you're trying to make something and WPF is a great framework. I love it. But I can spot one key problem in your second line of XAML and that is, your ItemsControl does not have an ItemsSource. Which means you must fill it programatically. Which means you create the "Content" Objects programatically. Which leads to the question why the Data in "Tag" is not in the Content already. I recommend you take a few hours and watch a few Videos on MVVM, especially the MVVM light toolkit. mvvmlight.net It can't be done in five minutes but it is well worth your time.
– themightylc
Nov 22 '18 at 7:37
1
1
Even if you get an answer to this, I'm afraid you are going down the rabbit hole! You should learn about Commands and maybe refine your understanding about MVVM in general. If you feel you don't need that for your project, WPF might not be the right choice. The Bindings look very suspicious to me, especially the Binding of Tag to Tag...
– themightylc
Nov 22 '18 at 6:44
Even if you get an answer to this, I'm afraid you are going down the rabbit hole! You should learn about Commands and maybe refine your understanding about MVVM in general. If you feel you don't need that for your project, WPF might not be the right choice. The Bindings look very suspicious to me, especially the Binding of Tag to Tag...
– themightylc
Nov 22 '18 at 6:44
@themightylc Can you please explain what is wrong with "Binding Tag"? I am using it for some data that is loaded from database, and since Buttons are generated during runtime, this is the way I am assigning data to certain button properties.
– Bodul
Nov 22 '18 at 6:52
@themightylc Can you please explain what is wrong with "Binding Tag"? I am using it for some data that is loaded from database, and since Buttons are generated during runtime, this is the way I am assigning data to certain button properties.
– Bodul
Nov 22 '18 at 6:52
There is nothing "wrong" with it. It is impossible to explain the concept of MVVM in a few words. But try to answer this yourself: What is the Binding Source and if it's an class you designed yourself, then why did you call the source Property "Tag"?
– themightylc
Nov 22 '18 at 7:22
There is nothing "wrong" with it. It is impossible to explain the concept of MVVM in a few words. But try to answer this yourself: What is the Binding Source and if it's an class you designed yourself, then why did you call the source Property "Tag"?
– themightylc
Nov 22 '18 at 7:22
I am guessing you are trying to achieve something like a gallery, maybe with pictures where you can move the pictures with your mouse but also something happens when you click on them, correct? Why would you need to Handle the Click Event, as well as MouseUp and MouseDown? Why does your Button have to be a Button?
– themightylc
Nov 22 '18 at 7:24
I am guessing you are trying to achieve something like a gallery, maybe with pictures where you can move the pictures with your mouse but also something happens when you click on them, correct? Why would you need to Handle the Click Event, as well as MouseUp and MouseDown? Why does your Button have to be a Button?
– themightylc
Nov 22 '18 at 7:24
1
1
to clarify: it's awesome you're trying to make something and WPF is a great framework. I love it. But I can spot one key problem in your second line of XAML and that is, your ItemsControl does not have an ItemsSource. Which means you must fill it programatically. Which means you create the "Content" Objects programatically. Which leads to the question why the Data in "Tag" is not in the Content already. I recommend you take a few hours and watch a few Videos on MVVM, especially the MVVM light toolkit. mvvmlight.net It can't be done in five minutes but it is well worth your time.
– themightylc
Nov 22 '18 at 7:37
to clarify: it's awesome you're trying to make something and WPF is a great framework. I love it. But I can spot one key problem in your second line of XAML and that is, your ItemsControl does not have an ItemsSource. Which means you must fill it programatically. Which means you create the "Content" Objects programatically. Which leads to the question why the Data in "Tag" is not in the Content already. I recommend you take a few hours and watch a few Videos on MVVM, especially the MVVM light toolkit. mvvmlight.net It can't be done in five minutes but it is well worth your time.
– themightylc
Nov 22 '18 at 7:37
|
show 6 more comments
2 Answers
2
active
oldest
votes
I totally agree with themightylc, but also understand you... WPF and MVVM are not so "easy" to get used to, I do it for a year more or less, and still have a lot to learn.
In that kind of situation I only could advise you to read some tutorials about WPF, DataBinding and ObservableCollection and ViewModel (these are the keywords you need to know).
1) Create a ViewModel where you can define a ObservableCollection, ObservableCollection is kind of list, but using it you can update your View (almost) automaticaly.so when you launch your application, you will read the list of buttons you need to display, then add them to the ObservableCollection
for your tests will be something like that :
Button button1=new Button();
Button button2=new Button();
//define all dimensions/parameters of your button
MyObservableCollection.Add(button1);
MyObservableCollection.Add(button2);
Then in XAML you just need to specify the ItemsSource of ItemsControls(MyObservableCollection). doing like that you don't need anymore all description of buttons inside.
Then when you click to add a button(in your case), you just need to make in code behind something like
Button newButton=new Button();
newButton.Height=defaultHeight...//width, background etc...
MyObservableCollection.Add(newButton);
again, just for advise if WPF/MVVM is new to you, I would advise to begin with easier samples, make a small listview with simple objects inside, or a listbox.
Could also advise you these websites :
wpf-tutorial.com
www.wpftutorial.net
add a comment |
At the end this is actually working properly.
For test I have set up a label, and in MouseMove event i am sending Mouse Position to that label,lblCoord.Content = Mouse.GetPosition(Application.Current.MainWindow);
In case of MouseClick I am sending lblCoord.Content="MouseClick";
And in case of MouseDown I am sending lblCoord.Content="MouseDown";
.
I can see mouse coordinates in lblCoord, I can see MouseClick, but it never displayed MouseDown.
However, if i call MessageBox inside MouseDown event, everything works. So i guess that XAML <Grid> PreviewMouseMove="onMouseMove"
works even when I am not moving mouse so it is sending coords to a Label all the time and overwrites lblCoord.Content="MouseDown";
faster than I am able to see it.
The answer to this question is: Don't work with WPF and expect WinForms results...
Thanks to everybody for their time and effort!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424549%2fwpf-button-event-not-firing-while-grid-have-previewmousemove-set%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I totally agree with themightylc, but also understand you... WPF and MVVM are not so "easy" to get used to, I do it for a year more or less, and still have a lot to learn.
In that kind of situation I only could advise you to read some tutorials about WPF, DataBinding and ObservableCollection and ViewModel (these are the keywords you need to know).
1) Create a ViewModel where you can define a ObservableCollection, ObservableCollection is kind of list, but using it you can update your View (almost) automaticaly.so when you launch your application, you will read the list of buttons you need to display, then add them to the ObservableCollection
for your tests will be something like that :
Button button1=new Button();
Button button2=new Button();
//define all dimensions/parameters of your button
MyObservableCollection.Add(button1);
MyObservableCollection.Add(button2);
Then in XAML you just need to specify the ItemsSource of ItemsControls(MyObservableCollection). doing like that you don't need anymore all description of buttons inside.
Then when you click to add a button(in your case), you just need to make in code behind something like
Button newButton=new Button();
newButton.Height=defaultHeight...//width, background etc...
MyObservableCollection.Add(newButton);
again, just for advise if WPF/MVVM is new to you, I would advise to begin with easier samples, make a small listview with simple objects inside, or a listbox.
Could also advise you these websites :
wpf-tutorial.com
www.wpftutorial.net
add a comment |
I totally agree with themightylc, but also understand you... WPF and MVVM are not so "easy" to get used to, I do it for a year more or less, and still have a lot to learn.
In that kind of situation I only could advise you to read some tutorials about WPF, DataBinding and ObservableCollection and ViewModel (these are the keywords you need to know).
1) Create a ViewModel where you can define a ObservableCollection, ObservableCollection is kind of list, but using it you can update your View (almost) automaticaly.so when you launch your application, you will read the list of buttons you need to display, then add them to the ObservableCollection
for your tests will be something like that :
Button button1=new Button();
Button button2=new Button();
//define all dimensions/parameters of your button
MyObservableCollection.Add(button1);
MyObservableCollection.Add(button2);
Then in XAML you just need to specify the ItemsSource of ItemsControls(MyObservableCollection). doing like that you don't need anymore all description of buttons inside.
Then when you click to add a button(in your case), you just need to make in code behind something like
Button newButton=new Button();
newButton.Height=defaultHeight...//width, background etc...
MyObservableCollection.Add(newButton);
again, just for advise if WPF/MVVM is new to you, I would advise to begin with easier samples, make a small listview with simple objects inside, or a listbox.
Could also advise you these websites :
wpf-tutorial.com
www.wpftutorial.net
add a comment |
I totally agree with themightylc, but also understand you... WPF and MVVM are not so "easy" to get used to, I do it for a year more or less, and still have a lot to learn.
In that kind of situation I only could advise you to read some tutorials about WPF, DataBinding and ObservableCollection and ViewModel (these are the keywords you need to know).
1) Create a ViewModel where you can define a ObservableCollection, ObservableCollection is kind of list, but using it you can update your View (almost) automaticaly.so when you launch your application, you will read the list of buttons you need to display, then add them to the ObservableCollection
for your tests will be something like that :
Button button1=new Button();
Button button2=new Button();
//define all dimensions/parameters of your button
MyObservableCollection.Add(button1);
MyObservableCollection.Add(button2);
Then in XAML you just need to specify the ItemsSource of ItemsControls(MyObservableCollection). doing like that you don't need anymore all description of buttons inside.
Then when you click to add a button(in your case), you just need to make in code behind something like
Button newButton=new Button();
newButton.Height=defaultHeight...//width, background etc...
MyObservableCollection.Add(newButton);
again, just for advise if WPF/MVVM is new to you, I would advise to begin with easier samples, make a small listview with simple objects inside, or a listbox.
Could also advise you these websites :
wpf-tutorial.com
www.wpftutorial.net
I totally agree with themightylc, but also understand you... WPF and MVVM are not so "easy" to get used to, I do it for a year more or less, and still have a lot to learn.
In that kind of situation I only could advise you to read some tutorials about WPF, DataBinding and ObservableCollection and ViewModel (these are the keywords you need to know).
1) Create a ViewModel where you can define a ObservableCollection, ObservableCollection is kind of list, but using it you can update your View (almost) automaticaly.so when you launch your application, you will read the list of buttons you need to display, then add them to the ObservableCollection
for your tests will be something like that :
Button button1=new Button();
Button button2=new Button();
//define all dimensions/parameters of your button
MyObservableCollection.Add(button1);
MyObservableCollection.Add(button2);
Then in XAML you just need to specify the ItemsSource of ItemsControls(MyObservableCollection). doing like that you don't need anymore all description of buttons inside.
Then when you click to add a button(in your case), you just need to make in code behind something like
Button newButton=new Button();
newButton.Height=defaultHeight...//width, background etc...
MyObservableCollection.Add(newButton);
again, just for advise if WPF/MVVM is new to you, I would advise to begin with easier samples, make a small listview with simple objects inside, or a listbox.
Could also advise you these websites :
wpf-tutorial.com
www.wpftutorial.net
answered Nov 22 '18 at 8:47
Siegfried.VSiegfried.V
189114
189114
add a comment |
add a comment |
At the end this is actually working properly.
For test I have set up a label, and in MouseMove event i am sending Mouse Position to that label,lblCoord.Content = Mouse.GetPosition(Application.Current.MainWindow);
In case of MouseClick I am sending lblCoord.Content="MouseClick";
And in case of MouseDown I am sending lblCoord.Content="MouseDown";
.
I can see mouse coordinates in lblCoord, I can see MouseClick, but it never displayed MouseDown.
However, if i call MessageBox inside MouseDown event, everything works. So i guess that XAML <Grid> PreviewMouseMove="onMouseMove"
works even when I am not moving mouse so it is sending coords to a Label all the time and overwrites lblCoord.Content="MouseDown";
faster than I am able to see it.
The answer to this question is: Don't work with WPF and expect WinForms results...
Thanks to everybody for their time and effort!
add a comment |
At the end this is actually working properly.
For test I have set up a label, and in MouseMove event i am sending Mouse Position to that label,lblCoord.Content = Mouse.GetPosition(Application.Current.MainWindow);
In case of MouseClick I am sending lblCoord.Content="MouseClick";
And in case of MouseDown I am sending lblCoord.Content="MouseDown";
.
I can see mouse coordinates in lblCoord, I can see MouseClick, but it never displayed MouseDown.
However, if i call MessageBox inside MouseDown event, everything works. So i guess that XAML <Grid> PreviewMouseMove="onMouseMove"
works even when I am not moving mouse so it is sending coords to a Label all the time and overwrites lblCoord.Content="MouseDown";
faster than I am able to see it.
The answer to this question is: Don't work with WPF and expect WinForms results...
Thanks to everybody for their time and effort!
add a comment |
At the end this is actually working properly.
For test I have set up a label, and in MouseMove event i am sending Mouse Position to that label,lblCoord.Content = Mouse.GetPosition(Application.Current.MainWindow);
In case of MouseClick I am sending lblCoord.Content="MouseClick";
And in case of MouseDown I am sending lblCoord.Content="MouseDown";
.
I can see mouse coordinates in lblCoord, I can see MouseClick, but it never displayed MouseDown.
However, if i call MessageBox inside MouseDown event, everything works. So i guess that XAML <Grid> PreviewMouseMove="onMouseMove"
works even when I am not moving mouse so it is sending coords to a Label all the time and overwrites lblCoord.Content="MouseDown";
faster than I am able to see it.
The answer to this question is: Don't work with WPF and expect WinForms results...
Thanks to everybody for their time and effort!
At the end this is actually working properly.
For test I have set up a label, and in MouseMove event i am sending Mouse Position to that label,lblCoord.Content = Mouse.GetPosition(Application.Current.MainWindow);
In case of MouseClick I am sending lblCoord.Content="MouseClick";
And in case of MouseDown I am sending lblCoord.Content="MouseDown";
.
I can see mouse coordinates in lblCoord, I can see MouseClick, but it never displayed MouseDown.
However, if i call MessageBox inside MouseDown event, everything works. So i guess that XAML <Grid> PreviewMouseMove="onMouseMove"
works even when I am not moving mouse so it is sending coords to a Label all the time and overwrites lblCoord.Content="MouseDown";
faster than I am able to see it.
The answer to this question is: Don't work with WPF and expect WinForms results...
Thanks to everybody for their time and effort!
answered Nov 23 '18 at 16:51
BodulBodul
918
918
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424549%2fwpf-button-event-not-firing-while-grid-have-previewmousemove-set%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Even if you get an answer to this, I'm afraid you are going down the rabbit hole! You should learn about Commands and maybe refine your understanding about MVVM in general. If you feel you don't need that for your project, WPF might not be the right choice. The Bindings look very suspicious to me, especially the Binding of Tag to Tag...
– themightylc
Nov 22 '18 at 6:44
@themightylc Can you please explain what is wrong with "Binding Tag"? I am using it for some data that is loaded from database, and since Buttons are generated during runtime, this is the way I am assigning data to certain button properties.
– Bodul
Nov 22 '18 at 6:52
There is nothing "wrong" with it. It is impossible to explain the concept of MVVM in a few words. But try to answer this yourself: What is the Binding Source and if it's an class you designed yourself, then why did you call the source Property "Tag"?
– themightylc
Nov 22 '18 at 7:22
I am guessing you are trying to achieve something like a gallery, maybe with pictures where you can move the pictures with your mouse but also something happens when you click on them, correct? Why would you need to Handle the Click Event, as well as MouseUp and MouseDown? Why does your Button have to be a Button?
– themightylc
Nov 22 '18 at 7:24
1
to clarify: it's awesome you're trying to make something and WPF is a great framework. I love it. But I can spot one key problem in your second line of XAML and that is, your ItemsControl does not have an ItemsSource. Which means you must fill it programatically. Which means you create the "Content" Objects programatically. Which leads to the question why the Data in "Tag" is not in the Content already. I recommend you take a few hours and watch a few Videos on MVVM, especially the MVVM light toolkit. mvvmlight.net It can't be done in five minutes but it is well worth your time.
– themightylc
Nov 22 '18 at 7:37