Image following 2 datatemplates:
<DataTemplate x:Key="templateGridDeleteBtn">
<Button Tag="{Binding stamnr}" Click="ButtonGridDelete_Click" ToolTipService.ToolTip="Verwijderen aanvraag mutatie">
<StackPanel VerticalAlignment="Center" Orientation="Horizontal">
<Image Source="../Images/DeleteRed.png" Width="20" Height="20" />
</StackPanel>
</Button>
</DataTemplate>
<DataTemplate x:Key="templateProcessRequest">
<Button Tag="{Binding stamnr}" Click="ButtonProcess_Click" ToolTipService.ToolTip="Verwerken zonder de Wasserij te contacteren">
<StackPanel VerticalAlignment="Center" Orientation="Horizontal">
<Image Source="../Images/Process.png" Width="20" Height="20" />
</StackPanel>
</Button>
</DataTemplate>
I'm having an Unhandled exception when adding the Click-event to my templateProcessRequest. Yet the code behind them is exactly the same (see below)
Private Sub ButtonGridDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
End Sub
Private Sub ButtonProcess_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
End Sub
If i remove the Click-Event from my second template then the exception isn't thrown. What is the reason of this strange behavior?
Found the problem. It had nothing to do with the XAML itself. The exception was thrown due to inheritance.
This control acts as parent for multiple other documents. And if the method shown in the Click event of the parameter isn't present in the 'sub'-controls then an unhandled exception is thrown.
Example:
MainControl A
=> Has method ButtonProcess_Click
SubControl B inherits MainControl A
=> Has method ButtonProcess_Click
SubControl C inherits MainControl A
=> Doesn't Have method ButtonProcess_Click
An exception will be thrown in SubControl C but not in A or B.
Related
I want to click the menu “Now Playing” then call UserControl to MainWindow's Grid
MainWindow.xaml:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Menu Height="30" VerticalAlignment="Top">
<MenuItem Height="30" Header="Now Playing" x:Name="NowPlaying" Click="NowPlaying_Click" />
<MenuItem Height="30" Header="Music Library" Click="MenuItem_Click" />
<MenuItem Height="30" Header="Play Schedule" />
<MenuItem Height="30" Header="Control" />
<MenuItem Height="30" Header="Option" />
</Menu>
<Grid Margin="0,30,0,0" x:Name="MainBoard">
(UserControl"NowPlaying")
</Grid>
</Grid>
MainWindow.xaml.vb
Private Sub NowPlaying_Click(sender As Object, e As RoutedEventArgs) Handles NowPlaying.Click
Dim MainWindow = TryCast(System.Windows.Window.GetWindow(Me), MainWindow)
Dim MainWindowBoard = MainWindow.MainBoard
Dim NowPlayingBoard As UserControl = New NowPlaying
#???
End Sub
UserControl.xaml:
<Grid>
<Grid Width="800" Margin="0,0,0,100"/>
<Grid Margin="0,350,0,0" Background="#FFFFDADA">
<Slider Margin="10,10,10,0" VerticalAlignment="Top" Height="30"/>
</Grid>
</Grid>
You add controls to a Panel e.g. Grid by using the Panel.Children property.
Your code can be simplified. As the event handler is defined in the code-behind, you already have direct access to all its fields and properties by simply using Me. Calling Window.GetWindow is redundant.
Private Sub NowPlaying_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim nowPlayingControl As UserControl = New NowPlaying()
Me.MainBoard.Children.Add(nowPlayingControl)
End Sub
My Answer:
Private Sub NowPlaying_Click(sender As Object, e As RoutedEventArgs) Handles NowPlaying.Click
Dim NowPlayingBoard As UserControl = New NowPlaying
Me.MainBoard.Children.Clear()
Me.MainBoard.Children.Add(NowPlayingBoard)
End Sub
Recently, I learned a lot about MVVM / Binding / Entityframework etc. And as I always go for the hard way - I use VB.NET - and have to convert most of the code found from C# to VB.NET ;)
So, what's my Topic:
Complete Titel:
WPF Hierarchical Treeview: Combined binding and templating of an selfrefering hierarchical source and a flat source with EntityFramework 6 and Database First Approach.
DataModel:
Image: https://i.stack.imgur.com/LIb1v.png
Expected Treeview
I have two types of Items:
"Dimensions" are from a selfrefering Source (XELL_DIMENSION) and
hierarchical Items. unknown/open Leveldepth.
"Elements" are from a flat Source (XELL_ELEMENTS) a
Image: https://i.stack.imgur.com/QeFwe.png
Ok here's what I have achieved so far:
Mainwindow Class:
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim elementsContext As New XELLEntities()
Tree.DataContext = elementsContext.XELL_DIMENSION.Include("XELL_ELEMS").ToList()
Tree.ItemsSource = elementsContext.XELL_DIMENSION.Where(Function(y) y.DIMEN_PARENT_ID Is Nothing).ToList()
End Sub
XAML CODE:
<TreeView Name="Tree" HorizontalAlignment="Left" Height="187" Margin="10,10,0,0" VerticalAlignment="Top" Width="415">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local2:XELL_DIMENSION}" ItemsSource="{Binding DIM_ALL_NODE}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding DIMEN_ID}"/>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding DIMEN_BEZ_LONG}"/>
<ListBox Name="Listy" ItemsSource="{Binding XELL_ELEMS}" DisplayMemberPath="ELEM_BEZ_LONG" BorderBrush="Transparent" BorderThickness="0"/>
</StackPanel>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
Result:
Question:
So that's the closest I came and I used a Listbox for the Elements - but this
is no solution to me.
- How do I solve my Problem?
I am thankful for any CodeSnipped provided.
Eureka! -SOLVED
I had to use a Converter and a TemplateSelector.
XAML:
<TreeView Name="Tree" HorizontalAlignment="Left" Height="187" Margin="10,10,0,0" VerticalAlignment="Top" Width="415"> <TreeView.Resources>
<local3:LeafDataTemplateSelector x:Key="LeafDataTemplateSelector" />
<HierarchicalDataTemplate DataType="{x:Type local2:XELL_DIMENSION}" ItemTemplateSelector="{StaticResource LeafDataTemplateSelector}">
<HierarchicalDataTemplate.ItemsSource>
<MultiBinding Converter="{StaticResource SimpleFolderConverter}">
<Binding Path="DIM_ALL_NODE" />
<Binding Path="XELL_ELEMS" />
</MultiBinding>
</HierarchicalDataTemplate.ItemsSource>
<StackPanel Orientation="Horizontal">
<Image HorizontalAlignment="Center" Height="20" VerticalAlignment="Center" Width="20" Source="MVVM/VIEW/IMAGES/Nodes.png" Stretch="Uniform" />
<TextBlock Foreground="#FF3399FF" Text="{Binding DIMEN_BEZ_LONG}" FontWeight="Bold"/>
</StackPanel>
</HierarchicalDataTemplate> <HierarchicalDataTemplate x:Key="Dimension" DataType="{x:Type local2:XELL_DIMENSION}" ItemTemplateSelector="{StaticResource LeafDataTemplateSelector}">
<HierarchicalDataTemplate.ItemsSource>
<MultiBinding Converter="{StaticResource SimpleFolderConverter}">
<Binding Path="XELL_ELEMS" />
<Binding Path="DIM_ALL_NODE" />
</MultiBinding>
</HierarchicalDataTemplate.ItemsSource>
<StackPanel Height="25" Orientation="Horizontal" ToolTip="Installation File">
<Image HorizontalAlignment="Center" Height="20" VerticalAlignment="Center" Width="20" Source="MVVM/VIEW/IMAGES/Nodes.png" Stretch="Uniform" />
<TextBlock Foreground="#FF3399FF" Text="{Binding DIMEN_BEZ_LONG}" FontWeight="Bold"/>
</StackPanel>
</HierarchicalDataTemplate> <DataTemplate x:Key="Element" DataType="{x:Type local2:XELL_ELEMENT}">
<StackPanel Height="25" Orientation="Horizontal" ToolTip="Installation File">
<Image HorizontalAlignment="Center" Height="20" VerticalAlignment="Center" Width="20" Source="MVVM/VIEW/IMAGES/Shape57.png" Stretch="Uniform" />
<TextBlock Foreground="DarkGray" Text="{Binding ELEM_BEZ_LONG}" FontWeight="Normal" FontStyle="Italic"/>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
VB.NET - MainWindow
Public Sub New()
' Dieser Aufruf ist für den Designer erforderlich.
InitializeComponent()
' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
End Sub
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim elementsContext As New XELLEntities()
Tree.DataContext = elementsContext.XELL_DIMENSION.Include("XELL_ELEMS").ToList()
Tree.ItemsSource = elementsContext.XELL_DIMENSION.Where(Function(y) y.DIMEN_PARENT_ID Is Nothing).ToList()
End Sub
VB.NET TemplateSelection
Public Class LeafDataTemplateSelector
Inherits DataTemplateSelector
Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate
Dim element As FrameworkElement = TryCast(container, FrameworkElement)
If element IsNot Nothing AndAlso item IsNot Nothing Then
If TypeOf item Is XELL_DIMENSION Then
Return TryCast(element.FindResource("Dimension"), DataTemplate)
ElseIf TypeOf item Is XELL_ELEMENT Then
Return TryCast(element.FindResource("Element"), DataTemplate)
End If
End If
Return Nothing
End Function
End Class
VB.NET Hierarchy Converter
Class HierarchyConverter : Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
Dim node = TryCast(value, Employee)
If node IsNot Nothing Then
Return node.Subordinates.Where(Function(i) i.ManagerID = node.EmployeeID).ToList()
Else
End If
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotSupportedException
End Function End Class
Hope this helps somebody ;)
I have a user control that contains a TextBox and I would like to capture the mousedown event however, I cannot seem to get things to work! My existing, non working code is below, any assistance would be greatly appreciated.
UserControl xaml:
<UserControl x:Class="LeftLabel"
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" Width="auto" Height="auto" >
<StackPanel DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=TextBlockText}"
Name="UcTextBlock"
Width="{Binding Path=TextBlockWidth}"
FontSize="{Binding Path=TextBlockFontSize}"
HorizontalAlignment="Right"
TextAlignment="Right"
VerticalAlignment="Center"
Margin="5,0,0,0" />
<TextBox Text="{Binding Path=TextBoxText}"
Name="UcTextBox"
MouseDown="UcTextBox_MouseDown"
Width="{Binding Path=TextBoxWidth}"
Height="{Binding Path=TextBoxHeight}"
FontSize="{Binding Path=TextBoxFontSize}"
Padding="{Binding Path=TextBoxPadding}"
Margin="5,0,0,0"
BorderThickness="0" />
</StackPanel>
</StackPanel>
</UserControl>
UserControl.vb:
Public Event TextBoxMouseDown As EventHandler
Private Sub UcTextBox_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles UcTextBox.MouseDown
RaiseEvent TextBoxMouseDown(sender, e)
End Sub
For testing purposes I am adding the UserControls to my MainWindow programmatically:
Dim count As Integer = 1
While count < 10
Dim ucl As New LeftLabel
With ucl
.Margin = New Thickness(4)
.TextBlockText = "Label " & count.ToString
.TextBlockWidth = 100
.TextBlockFontSize = 12
.TextBoxFontSize = 12
.TextBoxHeight = 20
.TextBoxText = "Initial Text " & count.ToString
.TextBoxPadding = New Thickness(2)
.TextBoxWidth = 150
AddHandler .TextBoxMouseDown, AddressOf LabelLeftTextBoxMouseDown
End With
TextBoxStackPanel.Children.Add(ucl)
count += 1
End While
Private Sub LabelLeftTextBoxMouseDown(sender As Object, e As EventArgs)
Dim txt As TextBox = DirectCast(sender, TextBox)
MsgBox(txt.Text)
End Sub
This is a somewhat common problem.
It occurs with some controls due to the fact that these controls handle these events internally. The button, for instance, "swallows" the click and rather exposes its own event - the Click-event.
If you want to declare your textbox-event-handlers in XAML, I suggest you checkout the Preview*-events (i.e. PreviewMouseDown), these always occur, maybe that can solve your problem if you need to react to clicks.
<TextBox Text="{Binding Path=TextBoxText}"
Name="UcTextBox"
PreviewMouseDown="UcTextBox_PreviewMouseDown"
Width="{Binding Path=TextBoxWidth}"
Height="{Binding Path=TextBoxHeight}"
FontSize="{Binding Path=TextBoxFontSize}"
Padding="{Binding Path=TextBoxPadding}"
Margin="5,0,0,0"
BorderThickness="0" />
I'm new to XAML. I wrote following routine to rotate an image by a given angle (0 to 360). I put in a slider control to set the angle based on slider value. Works great! However, when running the program and clicking the 'spin' button,a For/Next loop goes from 0 to 360 and the image will only display the last angle rotation (360). I did put in a Sleep command to slow, just in case I wasn't catching the previous updates. Any help with why it won't update continuously would be greatly appreciated. Thank you.
Imports System.Threading.Thread
Imports System.Windows.Media.Imaging.BitmapImage
Class MainWindow
Private Sub Slider1_ValueChanged(sender As System.Object, e As System.Windows.RoutedPropertyChangedEventArgs(Of System.Double)) Handles Slider1.ValueChanged
' ---- when I adjust manually, this works perfectly
Dim rotateTransform1 As New RotateTransform
rotateTransform1.Angle = Slider1.Value
lblAngle.Content = rotateTransform1.Angle
Image1.RenderTransform = rotateTransform1
End Sub
Private Sub btnSpin_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnSpin.Click
Dim spinAngle as Double
For SpinAngle 0 to 360
spinWheel(spinAngle)
Sleep(50)
Next spinAngle
End Sub
Private Sub spinWheel(ByVal spinAngle)
Dim rotateTransform1 As New RotateTransform
rotateTransform1.Angle = SpinAngle 'Slider1.Value
Image1.RenderTransform = rotateTransform1
lblAngle.Content = rotateTransform1.Angle
Image1.InvalidateVisual()
End Sub
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' Image1.createOption = BitmapCreateOptions.IgnoreImageCache
End Sub
End Class
XAML
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="677" Width="910">
<Grid Background="#FF006903">
<Grid.RowDefinitions>
<RowDefinition Height="238*" />
<RowDefinition Height="178*" />
</Grid.RowDefinitions>
<Button Content="SPIN!" Height="23" HorizontalAlignment="Left" Margin="521,101,0,0" Name="btnSpin" VerticalAlignment="Top" Width="75" Grid.Row="1" FontFamily="Tahoma" FontSize="15" FontWeight="ExtraBold" />
<Image Height="615" Margin="32,7,0,0" Name="Image1" Stretch="None" VerticalAlignment="Top"
RenderTransformOrigin=" 0.5,0.5" Source="/rotatePicture;component/Images/purp_wheel_cropped.png"
Grid.RowSpan="2" HorizontalAlignment="Left" Width="619" />
<Slider Height="25" HorizontalAlignment="Left" Margin="127,188,0,0" Name="Slider1" VerticalAlignment="Top" Width="350" Maximum="360" Grid.Row="1" />
<Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="521,175,0,0" Name="lblAngle" VerticalAlignment="Top" Width="75" Grid.Row="1" FontFamily="Tahoma" FontSize="15" FontWeight="ExtraBold" />
<Image Height="29" HorizontalAlignment="Left" Margin="535,252,0,0" Name="Image2" Stretch="Fill" VerticalAlignment="Top" Width="55" Source="/rotatePicture;component/Images/wheel_pointer.png" />
</Grid>
</Window>
The problem with your approach is that you are blocking the UI thread by repeatedly calling Sleep in a Click handler. WPF provides a very elegant mechanism for what you are trying to do. It's called Animation.
A method that animates the rotation of a FrameworkElement may look like shown below in C# (sorry, but I don't speak VB).
private void RotateElement(
FrameworkElement element, double from, double to, TimeSpan duration)
{
var transform = element.RenderTransform as RotateTransform;
if (transform != null)
{
var animation = new DoubleAnimation(from, to, duration);
transform.BeginAnimation(RotateTransform.AngleProperty, animation);
}
}
Note that the RotateTransform must already be contained in the RenderTransform property of the FrameworkElement. It could for example be assigned in XAML like this:
<Image RenderTransformOrigin="0.5,0.5" ...>
<Image.RenderTransform>
<RotateTransform/>
</Image.RenderTransform>
</Image>
You would call the RotateElement method in a Button Click handler like this:
private void btnSpin_Click(object sender, RoutedEventArgs e)
{
RotateElement(Image1, 0, 360, TimeSpan.FromMilliseconds(360 * 50));
}
Note also that in your Slider1_ValueChanged it is also not necessary to create a new RotateTransform every time.
Moreover, there is rarely any need to call InvalidateVisual, as you do in spinWheel.
Clemens answer is good. Do not forget that you can pause/resume animations but only if you create them in code-behind. If I remember correctly you can't control animations if you start them in XAML.
I know it works to read out the MP3 Tag attribute MP3V1 in silverlight. I know it works without any third party moduls. But when I use the MP3 workflow like Microsoft described I always get the count of zero attributes in my MP3 file.
How to solve?
Example project:
Download MP3 demo file from here:
http://www.file-upload.net/download-2617219/Elenor_techno.mp3.html
<UserControl x:Class="MP3_Tag_test.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White" Margin="12">
<StackPanel Orientation="Vertical">
<MediaElement x:Name="MediaHelper" Visibility="Visible" />
<TextBlock x:Name="txtTitle" Text="Title" FontSize="28" />
<Button Content="STOP" Height="23" HorizontalAlignment="Left" Name="Button3" Width="75" />
<Button Content="Play MP3" Height="23" HorizontalAlignment="Left" Name="Button1" VerticalAlignment="Top" Width="75" Margin="0,20,0,0" />
<Button Content="Play stream" Height="23" HorizontalAlignment="Left" Name="Button2" VerticalAlignment="Top" Width="75" />
</StackPanel>
</Grid>
Partial Public Class MainPage
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
End Sub
Private Sub MediaHelper_CurrentStateChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles MediaHelper.CurrentStateChanged
If Me.MediaHelper.Attributes.ContainsKey("Title") Then Me.txtTitle.Text = Me.MediaHelper.Attributes("Title")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Me.txtTitle.Text = "Loading mp3..."
Me.MediaHelper.Source = New Uri("http://dave-d.ch/database/music/public/1000036869_stefan%20lange%20koolja.mp3")
Me.MediaHelper.Play()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button2.Click
Me.txtTitle.Text = "Loading stream..."
Me.MediaHelper.Source = New Uri("http://asx.skypro.ch/radio/internet-128/fm1-nord.asx")
Me.MediaHelper.Play()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button3.Click
Me.MediaHelper.Stop()
End Sub
End Class
Ok. I created my own class and now I am submitting the MP3 Tag via service to Silverlight page. But I am still interested by Silverlight Tag reader does not work.