repeat button wpf not firing off click event - wpf

Hello community I have the following xaml.
<UserControl x:Class="Sample.SampleController"
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"
mc:Ignorable="d"
d:DesignHeight="410" d:DesignWidth="324">
<UserControl.Resources>
<Style x:Key="buttonON" TargetType="RepeatButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RepeatButton">
<Grid Width="80"
Height="80">
<Image Source="/Sample;component/icons/altitude_up_yellow.png"
Stretch="Fill" RenderOptions.BitmapScalingMode="Fant"/>
<ContentPresenter HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid Name="DriveGrid" Background="#C9000000" Width="324" Height="410">
<RepeatButton HorizontalAlignment="Left" x:Name="button1" VerticalAlignment="Top" IsEnabled="True" Style="{StaticResource buttonON}" Margin="232,297,0,0" Delay="100" Interval="200" Width="80" Height="80"/>
</Grid>
</UserControl>
And the following Code Behind
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Controls.Primitives;
namespace Sample {
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class SampleController : UserControl {
public SampleController() {
InitializeComponent();
button1.Click += new RoutedEventHandler(UpBtn_Click);
}
void UpBtn_Click(object sender, RoutedEventArgs e) {
Console.WriteLine("Click");
}
}
}
My question, why doesn't the repeat button fire off continously when I press and hold?
Thank you for your help.

To sum up comments. Even though I am not able to reproduce the problem stated in the question, as the code above works fine for me, I have found very similar issue in RepeatButton fires event only once! article. There is no definite answer what's causing the issue but it may give some clues to what to try

Related

if itemcontainerstyle is inside DataTemplate and a call the the main window is used an System.NullReferenceExc on start-up but works fine if ignored

when i try to call a event from the main wind if a control is in a template in itemcontainerstyle
when the run a System.NullReferenceException: 'Object reference not set to an instance of an object.' is displayed over InitializeComponent(); which if i continue it works fine as indented which is weird because an error message is normally there to tell you if something didn't work
below is the a cut down vision of my program only the things that case the error message to come up (i have tried it in different templates)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace wpf_test_zone
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void test(object sender, RoutedEventArgs e)
{
}
}
Title="MainWindow" Height="450" Width="800">
<Grid>
<TabControl x:Name="tabcontrol_1">
<TabControl.ContentTemplate>
<DataTemplate>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<TabControl x:Name="tabcontrol_2">
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="Header" Value="{Binding}"/>
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<Grid>
<Button x:Name="but" Click="test">
</Button>
</Grid>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
edit -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
the error message that comes up

wpf usercontrol resizing with thumb not firing DragDelta

I have created a Usercontrol that I would like to make resizable using the thumb control. I have added my control to a Canvas and when I click on the thumb control the only events that are fired are Dragend and DragStart (in that order) and DragDelta is not even firing. I have found someone with a similar problem Thumb inside an ItemsControl not firing DragDelta event but there is no way I can ask them if they found a solution.
This is the code :
<UserControl x:Class="test.ucChair"
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"
mc:Ignorable="d"
d:DesignHeight="53" d:DesignWidth="93" RenderTransformOrigin="0.5,0.5" Background="Beige"
>
<Grid Name="grd" Margin="0,0,0,0">
<Rectangle x:Name="rectAngle" Margin="0,0,0,0" Stroke="Black" RenderTransformOrigin="0.5,0.5" Grid.ZIndex="1000" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinHeight="53" /> <!-- Width="93" Height="53"-->
<TextBox x:Name="txtName" Text="OPEN" Margin="0,0,0,0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" IsReadOnly="True" IsReadOnlyCaretVisible="False" TextWrapping="Wrap" Width="93" KeyDown="txtName_KeyDown" FlowDirection="RightToLeft" BorderThickness="0" FontSize="14" Cursor="Hand" TextAlignment="Center"/>
<Thumb Name="thmbResize" DockPanel.Dock="Right" VerticalAlignment="Bottom" Height="15" Width="15"
DragDelta="OnResizeThumbDragDelta"
DragStarted="OnResizeThumbDragStarted"
DragCompleted="OnResizeThumbDragCompleted" HorizontalAlignment="Right">
</Thumb>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace test
{
/// <summary>
/// Interaction logic for ucChairLeft.xaml
/// </summary>
public partial class ucChair : UserControl
{
public ChairType AngleType;
//?? public int SectionID;
public ucChair()//ChairType CType)//?, int _SectionID)
{
InitializeComponent();
}
private void OnResizeThumbDragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("DragEnd");
Mouse.Capture(null);
}
private void OnResizeThumbDragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
System.Diagnostics.Debug.WriteLine("DragDelta");
}
private void OnResizeThumbDragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
Mouse.Capture(this);
System.Diagnostics.Debug.WriteLine("DragStart");
}
}
}
<Window xmlns:test="clr-namespace:test" x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300">
<Canvas Width="300" Height="300" Background="LightBlue">
<test:ucChair Canvas.Left="50" Canvas.Top="100" Width="50" Height="50" Background="Yellow"/>
</Canvas>
</Window>
Any help greatly appreciated!

How can I create a new usercontrol inherit from ComboBox?

I wanna to create a new usercontrol,it is inherit from ComboBox.I will add some new property to it, and rewrite its ControlTemplate.
Here is XAML:
<ComboBox x:Class="Pesticide.CustomUserControls.ComboxUserControl"
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:local="clr-namespace:Pesticide.CustomUserControls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<ComboBox.Resources>
<ControlTemplate TargetType="{x:Type ComboBox}" x:Key="{x:Type ComboBox}">
<Grid></Grid>
</ControlTemplate>
</ComboBox.Resources>
</ComboBox>
And here is code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Pesticide.CustomUserControls
{
public partial class ComboxUserControl : ComboBox
{
public ComboxUserControl()
{
InitializeComponent();
}
}
}
However,I just wrote these code,but WPF throw an error which is "InvalidCastException: Unable to cast object of type 'System.Windows.Controls.ControlTemplate' to type 'System.Windows.Style'."
What's wrong with my code?Would you please to help me?Thank you.
What's wrong with my code?
Using a type as resource key is the way WPF handles default Styles.
So a resource with
x:Key="{x:Type ComboBox}"
can't be a ControlTemplate. It has to be a Style.
However, it is simpler to directly set the Template property:
<ComboBox ...>
<ComboBox.Template>
<ControlTemplate TargetType="ComboBox">
...
</ControlTemplate>
</ComboBox.Template>
</ComboBox>

Integrate VLC player in C# (WPF) project using Vlc.DotNet

I want to integrate to my project a VLC player to display video cameras streams. For that, I try to use Vlc.DotNet (2.1.126 version) in my WPF project.
My tests are done in the following XAML file (I'm a bit a beginner at XAML/WPF):
<UserControl x:Class="TVSCS_View.VideoDisplay.VideoPlayerControl"
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:ctrl="clr-namespace:TVSCS_View.VideoDisplay"
xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300"
x:Name="controlVideoDisplay"
DataContext="{Binding ElementName=controlVideoDisplay}">
<Border BorderBrush="Black"
BorderThickness="1">
<Grid x:Name="videoDisplayLayoutRoot"
Margin="5,5,5,5">
<Image Source="{Binding ElementName=myVlcControl}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>
<ctrl:VideoCommandsControl x:Name="videoPlayerControl"
VerticalAlignment="Bottom"
Height="25"
Width="175"
Visibility="Visible"
Margin="10,0,10,20" />
<ctrl:VideoTimeLineControl x:Name="timeLineControl"
VerticalAlignment="Bottom"
Margin="0,0,0,0"/>
</Grid>
</Border>
</UserControl>
And the associated .cs file is:
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vlc.DotNet.Wpf;
namespace TVSCS_View.VideoDisplay
{
/// <summary>
/// Logique d'interaction pour VideoPlayerControl.xaml
/// </summary>
public partial class VideoPlayerControl : UserControl
{
public VlcControl myVlcControl;
public VideoPlayerControl()
{
InitializeComponent();
MediaPlayer media = new MediaPlayer();
myVlcControl = new VlcControl();
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
if (Environment.Is64BitOperatingSystem)
{
myVlcControl.MediaPlayer.VlcLibDirectory = new DirectoryInfo(System.IO.Path.Combine(currentDirectory, #"C:\Users\t0115019\Documents\Visual Studio 2015\Projects\tvscs_display\packages\VLC\"));
}
myVlcControl.MediaPlayer.EndInit();
myVlcControl.MediaPlayer.Play(new Uri("C:/Users/Documents/WP_20160908_11_16_53_Pro.mp4"));
}
}
}
Currently, I have an exception "FillNotFOundException" linked to "myVlcControl.MediaPlayer.EndInit()" line when I execute the application.
If I delete this line, nothing is displayed in the UserControl.
Nota:
I try to integrate the VlcControl using the following method:
<UserControl x:Class="TVSCS_View.VideoDisplay.VideoPlayerControl"
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:ctrl="clr-namespace:TVSCS_View.VideoDisplay"
xmlns:wpf="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300"
x:Name="controlVideoDisplay"
DataContext="{Binding ElementName=controlVideoDisplay}">
<Border BorderBrush="Black"
BorderThickness="1">
<Grid x:Name="videoDisplayLayoutRoot"
Margin="5,5,5,5">
<wpf:VlcControl x:Name="myVlcControl" />
</Grid>
</Border>
</UserControl>
But in this case, i have the following messsage:
the value of type "VlcControl" cannot be added to a collection or dictionary of type 'UIElementCollection'.
Do you have any solution for my little problem?
Thanks
The WPF version of the VlcControl is just a WindowsFormsHost control hosting the Windows Forms version of the VlcControl. Judging by the error message (The value of type "VlcControl" cannot be added to a collection or dictionary of type 'UIElementCollection') you're simply missing a reference to the WindowsFormsIntegration assembly, in which the WindowsFormsHost is defined (it can be found under Assemblies → Framework in the reference manager).
Here's a fully working example of a WPF window hosting the VLC player. You need to install the Vlc.DotNet.Wpf NuGet package (and its dependencies) and reference the WindowsFormsIntegration assembly.
MainWindow.xaml
<Window x:Class="HelloVlc.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vlc="clr-namespace:Vlc.DotNet.Wpf;assembly=Vlc.DotNet.Wpf">
<vlc:VlcControl x:Name="vlcPlayer" />
</Window>
MainWindow.xaml.cs
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
vlcPlayer.MediaPlayer.VlcLibDirectory =
//replace this path with an appropriate one
new DirectoryInfo(#"c:\Program Files (x86)\VideoLAN\VLC\");
vlcPlayer.MediaPlayer.EndInit();
vlcPlayer.MediaPlayer.Play(new Uri("http://download.blender.org/peach/" +
"bigbuckbunny_movies/big_buck_bunny_480p_surround-fix.avi"));
}
}

MP3 does not trigger the Storyboard.Completed event

I am trying to use the Storyboard's Completed event to indicate when an audio file has finished playing.
However, it seems .Net behaves differently when playing mp3 and wav files, and the event is triggered only for WAV.
Here is a XAML and code that demonstrate the problem:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
x:Name="mainWind">
<Window.Resources>
<Storyboard x:Key="mp3StoryBoard" Completed="Storyboard_Completed">
<MediaTimeline Source="001.mp3" Storyboard.TargetName="mediaEl" >
</MediaTimeline>
</Storyboard>
<Storyboard x:Key="wavStoryBoard" Completed="Storyboard_Completed">
<MediaTimeline Source="001.wav" Storyboard.TargetName="mediaEl" >
</MediaTimeline>
</Storyboard>
</Window.Resources>
<Grid>
<MediaElement x:Name="mediaEl" Height="120" HorizontalAlignment="Left" Margin="120,94,0,0" VerticalAlignment="Top" Width="160" />
<Button Content="Play MP3" Height="23" HorizontalAlignment="Left" Margin="148,260,0,0" Name="btnMP3" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Button Content="Play WAV" Height="23" HorizontalAlignment="Left" Margin="267,260,0,0" Name="btnWAV" VerticalAlignment="Top" Width="75" Click="btnWAV_Click" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Storyboard sb = (Storyboard)this.mainWind.Resources["mp3StoryBoard"];
sb.Begin();
}
private void Storyboard_Completed(object sender, EventArgs e)
{
MessageBox.Show(" Storyboard finished");
}
private void btnWAV_Click(object sender, RoutedEventArgs e)
{
Storyboard sb = (Storyboard)this.mainWind.Resources["wavStoryBoard"];
sb.Begin();
}
}
}
Will be glad if anyone has an idea for the reason for that and how to overcome it.
You don't have an attached event handler.
You need a
sb.Completed +=Storyboard_Completed;
before
The WPF MediaKit includes a MediaElement replacement, which may work better with mp3 files.

Resources