How to make click on last element?
Explanation : user click on button and move mouse in mode of click to another button. So click should be executed for last button, not for first one.
In WPF I have tried all possible events to get this (MouseUp seems to be right choice for this, but this doesn't do what I expect).
How to resolve this problem?
UPD: 2 Buttons in xaml:
<Button Grid.Column="1" Grid.Row="3" Click="ButtonThigh_OnClick" >
<Grid Width="150" Height="75">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Path=TrackingThighCircumference, Mode=OneWay}"/>
<TextBlock MouseRightButtonDown="Thigh_EditStarted" Height="65" Grid.RowSpan="2">
<TextBox Name="sizeThigh" Background="Transparent" Height="40" IsEnabled="False" ContextMenu="{x:Null}" ContextMenuService.IsEnabled="false" DataObject.Pasting="TextBox_Pasting" MaxLength="6" PreviewTextInput="TextBox_PreviewTextInput" LostFocus="TextBox_LostFocus" KeyUp="TextBox_KeyUp" Tag="ThighCircumferenceUI" Text="{Binding Path=ThighCircumferenceUI, Mode=TwoWay}"></TextBox>
<LineBreak/>
<TextBlock Name="ThighLb" Text="{x:Static strings:Resource.Thigh}"/><Span> (</Span><Span><TextBlock Text="{Binding ElementName=MeasurenentText, Path=Text}"/></Span><Span>)</Span>
</TextBlock>
</Grid>
</Button>
<Button Grid.Column="0" Grid.Row="4" Click="ButtonWaist_OnClick" >
<Grid Width="150" Height="75">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Path=TrackingJacketWaistCircumference, Mode=OneWay}"/>
<TextBlock Grid.Row="1" MouseRightButtonDown="Waist_EditStarted">
<TextBox Name="sizeWaist" Background="Transparent" Height="40" IsEnabled="False" ContextMenu="{x:Null}" ContextMenuService.IsEnabled="false" DataObject.Pasting="TextBox_Pasting" MaxLength="6" PreviewTextInput="TextBox_PreviewTextInput" LostFocus="TextBox_LostFocus" KeyUp="TextBox_KeyUp" Tag="JacketWaistCircumferenceUI" Text="{Binding Path=JacketWaistCircumferenceUI, Mode=TwoWay}"></TextBox>
<LineBreak/>
<TextBlock Name="WaistLb" Text="{x:Static strings:Resource.Waist}"/><Span> (</Span><Span><TextBlock Text="{Binding ElementName=MeasurenentText, Path=Text}"/></Span><Span>)</Span>
</TextBlock>
</Grid>
</Button>
Button Clicks :
private void ButtonThigh_OnClick(object sender, RoutedEventArgs e)
{
_resultViewModel.SelectMeasurement(si => si.ThighCircumference, vm => vm.ThighImg);
}
private void ButtonWaist_OnClick(object sender, RoutedEventArgs e)
{
_resultViewModel.SelectMeasurement(si => si.JacketWaistCircumference, vm => vm.WaistImg);
}
You can try it on buttons:
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("1");
}
private void button2_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("2");
}
private void Button1_OnPreviewMouseUp(object sender, MouseButtonEventArgs e)
{
button1_Click(sender,e);
}
private void Button2_OnPreviewMouseUp(object sender, MouseButtonEventArgs e)
{
button2_Click(sender,e);
}
private void Button1_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
private void Button2_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
After that, your PreviewMouseUp will work as you expect:
Related
i want to send Data from one Textbox on window One to a label on window Two.
starting with window two:
<StackPanel>
<StackPanel x:Name="ButtonStackPanel" Height="Auto" Width="Auto">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<Button Style="{DynamicResource ButtonStyle}" Content="To The Dark Side" Click="OnClickToDarkSide"/>
<Button Style="{DynamicResource ButtonStyle}" Content="To The Gray" Click="OnClickToGraySide"/>
<Button Style="{DynamicResource ButtonStyle}" Content="To The Light Side" Click="OnClickToLightSide"/>
</StackPanel>
<Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
<Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock" Content="{Binding Source=CodeText}"/>
<Border HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Red" Height="Auto" Width="2"/>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Style Window" Name="StyleWindowButton" Click="OnClickOpenStyleWindow"/>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}" Content="Open Text Window" Name="TextWindowButton" Click="OnClickOpenTextWindow"/>
</StackPanel>
<Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>
<Border Height="2" Width="Auto" Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</StackPanel>
Codebehind of Window Two:
public MainWindow()
{
(App.Current as App).CodeText = _jediCode;
InitializeComponent();
}
private void OnClickToDarkSide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Dark);
(App.Current as App).CodeText = _sithCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickToLightSide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Light);
(App.Current as App).CodeText = _jediCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickToGraySide(object sender, RoutedEventArgs e)
{
(App.Current as App).ChangeSkin(Skin.Gray);
(App.Current as App).CodeText = _grayCode;
theTextBlock.Content = (App.Current as App).CodeText;
}
private void OnClickOpenStyleWindow(object sender, RoutedEventArgs e)
{
if (StyleWindowButton.IsChecked == true)
{
styleWindow = new StyleWindow();
styleWindow.Show();
}
else
{
styleWindow.Close();
styleWindow = null;
}
}
private void OnClickOpenTextWindow(object sender, RoutedEventArgs e)
{
if (TextWindowButton.IsChecked == true)
{
textWindow = new InputWindow();
textWindow.Show();
}
else
{
textWindow.Close();
textWindow = null;
}
}
}
window one:
<Grid>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="200" TextWrapping="Wrap"
AcceptsReturn="True" AcceptsTab="True" Text="{Binding Path=CodeText, Source={x:Static Application.Current}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Margin="10,10,0,0">
<!-- TODO: trigger change from this textbox to the textblock in mainwindow -->
</TextBox>
</Grid>
code behind of one is empty.
app.xaml.cs:
public string CodeText
{
get => _codeText;
set { _codeText = value; OnPropertyChanged(nameof(CodeText)); }
}
Ok, the current behavior is clicking on one of the buttons (Dark Side, Gray, Light Side) leads to changes in the CodeText Property, which leads to a change of the content of the label of Window Two and the text of TextBox of Window One. Changing the text of the TextBox, changes also the CodeText Property, but does not lead to a change in the label and thats confusing, why does it work the one way, but not the other.
hope you have a hint for me. :) Maybe i missed a trigger or a kind of refresh for the label
bindings in window One and Two are set differently. (window Two does it wrong, Content="{Binding Source=CodeText}" is not valid binding)
in fact, window Two removes the binding by assigning CodeText directly as local value:
theTextBlock.Content = (App.Current as App).CodeText;
you should remove that line, and use the same binding as in window One:
<Label Style="{DynamicResource LabelStyle}" x:Name="theTextBlock"
Content="{Binding Path=CodeText, Source={x:Static Application.Current}}"/>
I'm using:
<WebBrowser Height="387" VerticalAlignment="Top" Width="468" Name="Camera1" />
I'm have and button start in XAML:
<Button x:Name="startButton" Content="Start" Width="75" Height="20" Margin="4,0,0,0" Click="startButton_Click" />
In xaml.cs class I have method startButton_Click:
private void startButton_Click(object sender, RoutedEventArgs e)
{
Camera1.Navigate("http://96.10.1.168/mjpg/1/video.mjpg?timestamp=1470753862585");
}
I want to open an Edit-page i've created when i click on the MenuFlyoutItem. Heres the code for my ListView:
<ListView Name="ModelListXAML" Grid.Row="1" Margin="0,12,0,0" CanDragItems="True" SelectionChanged="ModelListXAML_SelectionChanged" ItemClick="ModelListXAML_ItemClick">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,0,5" Holding="ModellItem_Holding">
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyoutItem x:Name="ModellMenuEdit" Click="ModellMenuEdit_Click" Text="Bearbeiten"/>
<MenuFlyoutItem x:Name="ModellMenuDelete" Click="ModellMenuDelete_Click" Text="Löschen"/>
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<StackPanel Margin="10, 0,0,0">
<TextBlock Text="{Binding name}" FontFamily="Segoe WP Semibold" FontSize="30" HorizontalAlignment="Left" Grid.Row="0"/>
<TextBlock Text="{Binding captions}" FontFamily="Segoe WP Semibold" FontSize="20" HorizontalAlignment="Left" Grid.Row="1" Foreground="Gray"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
My Problem is, that the app crashes, when the ModellMenuEdit_Click event get's fired. Visual Studio gives me this Error:
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
And here's the Code for the ModelMenuEdit_Click event:
private void ModellMenuEdit_Click(object sender, RoutedEventArgs e)
{
Modell datacontext = (e.OriginalSource as FrameworkElement).DataContext as Modell;
Frame.Navigate(typeof(ModellEdit));
}
Any idea how to solve this? Thank's for any response in advance :)
The problem is with the way you're trying to retrieve the DataContext. Try this:
private void ModellMenuEdit_Click(object sender, RoutedEventArgs e)
{
var menuFlyoutItem = sender as MenuFlyoutItem;
Modell datacontext;
if (menuFlyoutItem != null)
{
datacontext = menuFlyoutItem.DataContext as Modell;
}
Frame.Navigate(typeof(ModellEdit), /*in case you want to pass datacontext*/ datacontext);
}
I've solved it! The actual problem was in the OnNavigatedTo() Method for the ModellEdit page :)
I'm trying to make a Grid that has options to choose the number of players (up to six) and input names in TextBoxes. There are "+" and "-" buttons under the grid that will add or remove a player (it starts with two). Everything works fine until you add the 6th player and then try to subtract 1 player. The Player 6 Textbox and Name input Textbox stay visible, replacing the Player 5 ones. Here is the xaml
<Grid HorizontalAlignment="Center" VerticalAlignment="Center" Width="800" Grid.Row="1" >
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!--Headers-->
<TextBlock Style="{ThemeResource HeaderTextBlockStyle}" Grid.Row="0" Grid.Column="0" Text=""/>
<TextBlock Style="{ThemeResource HeaderTextBlockStyle}" Grid.Row="0" Grid.Column="1" Text="Name" Margin="20,0,20,0" TextAlignment="Center"/>
<TextBlock Name="PlayerOneTag" Text="Player 1" FontSize="30" Grid.Row="1" Grid.Column="0" Margin="0,20,40,20" TextAlignment="Left"/>
<TextBlock Name="PlayerTwoTag" Text="Player 2" FontSize="30" Grid.Row="2" Grid.Column="0" Margin="0,20,40,20" TextAlignment="Left"/>
<TextBlock Name="PlayerThreeTag" Text="Player 3" FontSize="30" Grid.Row="3" Grid.Column="0" Margin="0,20,40,20" TextAlignment="Left" Visibility="Collapsed"/>
<TextBlock Name="PlayerFourTag" Text="Player 4" FontSize="30" Grid.Row="4" Grid.Column="0" Margin="0,20,40,20" TextAlignment="Left" Visibility="Collapsed"/>
<TextBlock Name="PlayerFiveTag" Text="Player 5" FontSize="30" Grid.Row="5" Grid.Column="0" Margin="0,20,40,20" TextAlignment="Left" Visibility="Collapsed"/>
<TextBlock Name="PlayerSixTag" Text="Player 6" FontSize="30" Grid.Row="6" Grid.Column="0" Margin="0,20,40,20" TextAlignment="Left" Visibility="Collapsed"/>
<TextBox Name="PlayerOneName" FontSize="30" Grid.Row="1" Grid.Column="1" />
<TextBox Name="PlayerTwoName" FontSize="30" Grid.Row="2" Grid.Column="1" />
<TextBox Name="PlayerThreeName" FontSize="30" Grid.Row="3" Grid.Column="1" Visibility="Collapsed" />
<TextBox Name="PlayerFourName" FontSize="30" Grid.Row="4" Grid.Column="1" Visibility="Collapsed" />
<TextBox Name="PlayerFiveName" FontSize="30" Grid.Row="5" Grid.Column="1" Visibility="Collapsed" />
<TextBox Name="PlayerSixName" FontSize="30" Grid.Row="6" Grid.Column="1" Visibility="Collapsed"/>
<StackPanel Name="PlayerTwoButtons" Grid.Row="7" Orientation="Horizontal">
<Button Name="AddPlayerThreeButton" Tapped="AddPlayerThree" Style="{StaticResource ResourceKey=PlusButton}" Visibility="Visible"/>
</StackPanel>
<StackPanel Name="PlayerThreeButtons" Visibility="Collapsed" Grid.Row="7" Orientation="Horizontal">
<Button Name="MinusPlayerThreeButton" Tapped="MinusPlayerThree" Style="{StaticResource ResourceKey=MinusButton}" />
<Button Name="AddPlayerFourButton" Tapped="AddPlayerFour" Style="{StaticResource ResourceKey=PlusButton}" />
</StackPanel>
<StackPanel Name="PlayerFourButtons" Visibility="Collapsed" Grid.Row="7" Orientation="Horizontal" >
<Button Name="MinusPlayerFourButton" Tapped="MinusPlayerFour" Style="{StaticResource ResourceKey=MinusButton}"/>
<Button Name="AddPlayerFiveButton" Tapped="AddPlayerFive" Style="{StaticResource ResourceKey=PlusButton}" />
</StackPanel>
<StackPanel Name="PlayerFiveButtons" Grid.Row="7" Orientation="Horizontal" Visibility="Collapsed">
<Button Name="MinusPlayerFiveButton" Tapped="MinusPlayerFive" Style="{StaticResource ResourceKey=MinusButton}" />
<Button Name="AddPlayerSixButton" Tapped="AddPlayerSix" Style="{StaticResource ResourceKey=PlusButton}" />
</StackPanel>
<StackPanel Name="PlayerSixButtons" Grid.Row="7" Orientation="Horizontal" Visibility="Collapsed">
<Button Name="MinusPlayerSixButton" Tapped="MinusPlayerSix" Style="{StaticResource ResourceKey=MinusButton}" />
</StackPanel>
</Grid>
And here is the C# code behind
private void AddPlayerThree(object sender, TappedRoutedEventArgs e)
{
PlayerThreeName.Visibility = Visibility.Visible;
PlayerThreeTag.Visibility = Visibility.Visible;
PlayerTwoButtons.Visibility = Visibility.Collapsed;
PlayerThreeButtons.Visibility = Visibility.Visible;
}
private void AddPlayerFour(object sender, TappedRoutedEventArgs e)
{
PlayerFourName.Visibility = Visibility.Visible;
PlayerFourTag.Visibility = Visibility.Visible;
PlayerThreeButtons.Visibility = Visibility.Collapsed;
PlayerFourButtons.Visibility = Visibility.Visible;
}
private void AddPlayerFive(object sender, TappedRoutedEventArgs e)
{
PlayerFiveName.Visibility = Visibility.Visible;
PlayerFiveTag.Visibility = Visibility.Visible;
PlayerFourButtons.Visibility = Visibility.Collapsed;
PlayerFiveButtons.Visibility = Visibility.Visible;
}
private void AddPlayerSix(object sender, TappedRoutedEventArgs e)
{
PlayerSixName.Visibility = Visibility.Visible;
PlayerSixTag.Visibility = Visibility.Visible;
AddPlayerSixButton.Visibility = Visibility.Collapsed;
MinusPlayerSixButton.Visibility = Visibility.Visible;
}
private void MinusPlayerThree(object sender, TappedRoutedEventArgs e)
{
PlayerThreeName.Visibility = Visibility.Collapsed;
PlayerThreeTag.Visibility = Visibility.Collapsed;
PlayerThreeButtons.Visibility = Visibility.Collapsed;
PlayerTwoButtons.Visibility = Visibility.Visible;
}
private void MinusPlayerFour(object sender, TappedRoutedEventArgs e)
{
PlayerFourName.Visibility = Visibility.Collapsed;
PlayerFourTag.Visibility = Visibility.Collapsed;
PlayerFourButtons.Visibility = Visibility.Collapsed;
PlayerThreeButtons.Visibility = Visibility.Visible;
}
private void MinusPlayerFive(object sender, TappedRoutedEventArgs e)
{
PlayerFiveName.Visibility = Visibility.Collapsed;
PlayerFiveTag.Visibility = Visibility.Collapsed;
PlayerFiveButtons.Visibility = Visibility.Collapsed;
PlayerFourButtons.Visibility = Visibility.Visible;
}
private void MinusPlayerSix(object sender, TappedRoutedEventArgs e)
{
PlayerSixTag.Visibility = Visibility.Collapsed;
PlayerSixName.Visibility = Visibility.Collapsed;
PlayerSixButtons.Visibility = Visibility.Collapsed;
PlayerFiveButtons.Visibility = Visibility.Visible;
}
Any ideas on how to fix this and I would really appreciate it!
When you add player 6, you're not doing the same things as in the other buttons' clicks (I compared the different button clicks/taps, then saw the problem). In AddPlayerSix you worked directly on the Buttons, instead of on the StackPanels. The code below works:
private void AddPlayerSix(object sender, TappedRoutedEventArgs e)
{
PlayerSixName.Visibility = Visibility.Visible;
PlayerSixTag.Visibility = Visibility.Visible;
// Your code (not working)
//AddPlayerSixButton.Visibility = Visibility.Collapsed;
//MinusPlayerSixButton.Visibility = Visibility.Visible;
// New code (works)
PlayerFiveButtons.Visibility = Visibility.Collapsed;
PlayerSixButtons.Visibility = Visibility.Visible;
}
I have something like this:
XAML (for one item):
<ListView Margin="10,0,10,10" Width="360" Height="510" VerticalAlignment="Bottom" RenderTransformOrigin="0.479,0.497">
<ListViewItem RenderTransformOrigin="0.719,0.534">
<StackPanel Orientation="Horizontal">
<Image Source="/Assets/1.png" Margin="0,0,5,0" />
<TextBlock FontSize="20" Width="302" RenderTransformOrigin="0.698,0.49" SelectionChanged="TextBlock_SelectionChanged" IsHoldingEnabled="False" IsDoubleTapEnabled="False">
<Run Text="Stotis–Oro uostas"/>
</TextBlock>
C#:
private void TextBlock_SelectionChanged(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(SecondPage));
}
I want to make like that: When I click on 1st item it should send me to another page.
You have to add tapped listener to the listview items like this:
<ListView Margin="10,0,10,10" Width="360" Height="510" VerticalAlignment="Bottom" RenderTransformOrigin="0.479,0.497">
<ListViewItem x:Name="ListViewItem1" Tapped="ListViewItem1_Tapped">
<!--add your image and text and ...-->
</ListViewItem>
<ListViewItem x:Name="ListViewItem2" Tapped="ListViewItem2_Tapped">
...
</ListViewItem>
<ListViewItem x:Name="ListViewItem3" Tapped="ListViewItem3_Tapped">
...
</ListViewItem>
</ListView>
Than you can add in the code behind file this:
private void ListViewItem1_Tapped(object sender, TappedRoutedEventArgs e)
{
this.Frame.Navigate(typeof(FirstPage));
}
private void ListViewItem2_Tapped(object sender, TappedRoutedEventArgs e)
{
this.Frame.Navigate(typeof(SecondPage));
}
private void ListViewItem3_Tapped(object sender, TappedRoutedEventArgs e)
{
this.Frame.Navigate(typeof(ThirdPage));
}
I hope this is like you wanted it.