thats my navigation window
<NavigationWindow 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="800" Width="600" Source="Page1.xaml">
thats my page1
<Page x:Class="WpfApplication1.Page1"
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="600" d:DesignWidth="800"
Title="Page1" Name="IndexPage">
<ListView Name="myListView" ItemsSource="{Binding ElementName=IndexPage, Path=SeriesCollection}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" IsSynchronizedWithCurrentItem="True" SelectionChanged="handleSelected">
<ListView.ItemsPanel >
<ItemsPanelTemplate>
<WrapPanel>
</WrapPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel >
<Image Width="214" Height="317" Source="{Binding Image}"/>
<Label Content="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Page 2 is just en empty skeleton
code behind
namespace WpfApplication1
{
/// <summary>
/// Interaktionslogik für Page1.xaml
/// </summary>
public partial class Page1 : Page
{
private ObservableCollection<Series> _series =
new ObservableCollection<Series>();
public ObservableCollection<Series> SeriesCollection
{
get { return _series; }
}
public Page1()
{
InitializeComponent();
DirectoryInfo baseDir = new DirectoryInfo(#"C:\Serien");
DirectoryInfo[] dirs = baseDir.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
Series serie = new Series(dir);
Console.WriteLine("adding " + serie.Name);
_series.Add(serie);
}
Console.WriteLine(_series.Count);
}
public void handleSelected(object sender, RoutedEventArgs args)
{
Series currentSerie = (Series) myListView.Items.CurrentItem;
Page2 page = new Page2();
this.NavigationService.Navigate(page);
Console.WriteLine(currentSerie.Name);
Console.WriteLine(currentSerie.GetType());
Console.WriteLine(currentSerie.ToString());
}
}
}
so i click on an item to trigger the SelectionChanged Event to handle it in SelectionChanged where i navigate to page2 , so far so good.
then i use the back button from the navigation window and get stuck with an NullpointerException at
this.NavigationService.Navigate(page);
i dont even know why this method is triggered. So obviosly i am doing something stupid. Pls tell me what it is. Thanks for your time and affort.
The problem here is that you handle the wrong event. I assume that you want to open Page2 by clicking a ListViewItem. Therefore you should use mouse events instead of SelectionChanged.
For example, you can subscribe to StackPanel MouseDown event in your DataTemplate:
<DataTemplate>
<StackPanel Background="Transparent"
MouseDown="StackPanel_MouseDown">
<Image Width="214" Height="317" Source="{Binding Image}"/>
<Label Content="{Binding Name}"/>
</StackPanel>
</DataTemplate>
You can access clicked Series using the following:
private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
var currentSerie = (Series)((StackPanel)sender).DataContext;
...
}
UPD If you need a real click, you may use a trick like this:
<DataTemplate>
<Button Click="Button_Click">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter/>
</ControlTemplate>
</Button.Template>
<StackPanel Background="Transparent">
<Image Width="214" Height="317" Source="{Binding Image}"/>
<Label Content="{Binding Name}"/>
</StackPanel>
</Button>
</DataTemplate>
We use a Button like a view-model which is able to handle clicks.
Related
I need help accessing in C# a TreeView created in .xaml.
I'm creating a simple TreeView that will collect all "Layers" from a document, and populate the TreeView with their names and other properties.
As a start point, I just want the names, then I'll add more properties.
The .xaml is as follows:
<Window x:Class="TestWPFAlpha.DocumentStructureWindow"
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"
xmlns:local="clr-namespace:TestAlpha"
mc:Ignorable="d"
Closing="Window_Closing"
Title=" Document Structure" Height="600" MinHeight="600" MaxHeight="600" Width="400" MinWidth="400" MaxWidth="400" ScrollViewer.VerticalScrollBarVisibility="Visible">
<Grid x:Name="gridDocumentStructure" x:FieldModifier="public">
<Menu Height="25" Margin="0,0,0,0" VerticalAlignment="Top" FontSize="14" Background="#FF3C4B64" Foreground="White">
<MenuItem Header="Search" />
<MenuItem Header="Close" Height="25" Click="buttonCloseDocumentStructureWindow_Click"/>
</Menu>
<TreeView x:Name="TreeViewDocumentStructure" x:FieldModifier= "public" Background="#FFEBEBEB" Margin="10,35,10,10">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Members}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
In C# I have:
// all necessary using statements and the two below, that are specific thar I need in this method
using static TestWPFAlpha.DocumentStructureWindow;
using static DocumentLayers.LayerManagerWindow;
...
and an event for a button as below:
public static void DocumentStructure_Click(object sender, RoutedEventArgs e)
{
if (documentStructureWindow == null)
{
documentStructureWindow = new TestWPFAlpha.DocumentStructureWindow();
documentStructureWindow.Show();
}
else
{
documentStructureWindow.Activate();
}
var tds = new TestWPFAlpha.DocumentStructureWindow.gridDocumentStructure.TreeViewDocumentStructure();
System.Type canvasType = typeof(System.Windows.Controls.Canvas);
int count = 0;
foreach (UIElement uie in TestWPFAlpha.MainWindow.canvasGrid.Children)
{
if (uie.GetType() == canvasType && count > 0)
{
sb.AppendLine(Layers[count - 1].Name);
TreeViewItem newChild = new TreeViewItem();
// Layers are from DocumentLayers.LayerManagerWindow
newChild.Header = Layers[count - 1].Name;
tds.Items.Add(newChild);
}
count++;
}
}
At line 49
var tds = new TestWPFAlpha.DocumentStructureWindow.gridDocumentStructure.TreeViewDocumentStructure();
I get the error:
The type name 'gridDocumentStructure' does not exist in the type 'DocumentStructureWindow'
Help greatly appreciated.
An example as you requested. It's just a basic thing using code behind
the window with the tree view:
<Window x:Class="TestWPFAlpha.DocumentStructureWindow"
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"
xmlns:local="clr-namespace:TestWPFAlpha"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid x:Name="gridDocumentStructure" x:FieldModifier="public">
<Menu Height="25" Margin="0,0,0,0" VerticalAlignment="Top" FontSize="14" Background="#FF3C4B64" Foreground="White">
<MenuItem Header="Search" />
<MenuItem Header="Close" Height="25" Click="buttonCloseDocumentStructureWindow_Click"/>
</Menu>
<TreeView x:Name="TreeViewDocumentStructure" x:FieldModifier= "public" Background="#FFEBEBEB" Margin="10,35,10,10">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Members}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
it creates a new window:
usings...;
namespace TestWPFAlpha
{
public partial class DocumentStructureWindow : Window
{
public DocumentStructureWindow()
{
InitializeComponent();
var newWin = new Window1(this);
newWin.Show();
}
}
}
and the newWin has a button, which does what I think you would like to achieve:
public partial class Window1 : Window
{
private DocumentStructureWindow _documentStructureWindow;
public Window1(DocumentStructureWindow documentStructureWindow)
{
InitializeComponent();
_documentStructureWindow = documentStructureWindow;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var tds = _documentStructureWindow.TreeViewDocumentStructure;
tds.Items.Add(new TreeViewItem() { Header = DateTime.Now.ToString("yyMMdd_HHmmss_fff")} );
}
}
see it here: repo
But I would recommend mvvm & commands instead of this code behind solution
I'm attempting to use the ContinuumNavigationTransition effect in a Windows 10 UWP app. I can get the EntranceElement to correctly fly in when navigating pages, but the ExitElement never animates out.
As a repro case, I made a bare minimum app, with still no success. My code looks like the following:
MainPage.xaml
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Center">
<ListView x:Name="TheList"
ContinuumNavigationTransitionInfo.ExitElementContainer="True"
ItemsSource="{x:Bind SomeItems}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"
Style="{StaticResource HeaderTextBlockStyle}"
ContinuumNavigationTransitionInfo.IsExitElement="True"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Content="Navigate"
HorizontalAlignment="Center"
Tapped="Button_Tapped"/>
</StackPanel>
</Grid>
MainPage.xaml.cs
public ObservableCollection<String> SomeItems = new ObservableCollection<string>
{
"Item 1!",
"Item 2!"
};
public MainPage()
{
this.InitializeComponent();
}
private void Button_Tapped(object sender, TappedRoutedEventArgs e)
{
((Frame)Window.Current.Content).Navigate(typeof(SubPage));
}
SubPage.xaml
<Page.Transitions>
<TransitionCollection>
<NavigationThemeTransition>
<NavigationThemeTransition.DefaultNavigationTransitionInfo>
<ContinuumNavigationTransitionInfo/>
</NavigationThemeTransition.DefaultNavigationTransitionInfo>
</NavigationThemeTransition>
</TransitionCollection>
</Page.Transitions>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock Text="I'm an entrance element!"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContinuumNavigationTransitionInfo.IsEntranceElement="True"
TextWrapping="WrapWholeWords"
Style="{StaticResource SubheaderTextBlockStyle}"/>
</Grid>
SubPage.xaml.cs
public SubPage()
{
this.InitializeComponent();
SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
SystemNavigationManager.GetForCurrentView().BackRequested += SubPage_BackRequested;
}
private void SubPage_BackRequested(object sender, BackRequestedEventArgs e)
{
((Frame)Window.Current.Content).Navigate(typeof(MainPage));
SystemNavigationManager.GetForCurrentView().BackRequested -= SubPage_BackRequested;
}
I believe where you are going wrong is that you are using a button to navigate whereas Entrance elements and exit elements are used to kind of carry the context forward. So ideally you should navigate on listview item click.Code wise I couldn't see anything wrong and check this link it might be of some use.
http://www.visuallylocated.com/post/2014/06/24/Page-transitions-and-animations-in-Windows-Phone-Runtime.aspx
I currently have the following XAML code but when I run my app, the ScrollBars appear but I am unable to scroll through the list of images (scrollbar doesn't work).
<Window x:Class="WPFMediaManager.MoviePanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MoviePanel" Height="1024" Width="1473.254" WindowStartupLocation="CenterScreen">
<Window.Resources>
<DataTemplate x:Key="ItemTemplate">
<WrapPanel>
<Image Width="200" Height="300" Stretch="Fill" Source="{Binding}"/>
<TextBlock Text="{Binding}"/>
</WrapPanel>
</DataTemplate>
</Window.Resources>
<Grid x:Name="movie_grid">
<ListView Grid.Row="4" Name ="MovieListView" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding Path = movie_posters_list}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<TextBlock Name="SampleTextBlock" Text="{Binding Path=movie_names}" DataContext="{StaticResource ItemTemplate}"/>
</Grid>
</Window>
I'm not sure what is causing this issue and whether I'm using the appropriate containers to house the images.
My goal is something like the following layout:
C# Code behind:
namespace WPFMediaManager {
/// <summary>
/// Interaction logic for MoviePanel.xaml
/// </summary>
public partial class MoviePanel : Window {
public MoviePanel() {
InitializeComponent();
}
List<ImageSource> movie_posters_list = new List<ImageSource>();
List<String> movie_names = new List<String>();
String regex_pattern = #"\\([\w ]+).(?:jpg|png)$";
public void LoadImages() {
//Image current_image;
String movie_poster_path = #"C:\Users\Vax\Desktop\movie_posters";
List<String> filenames = new List<String>(System.IO.Directory.EnumerateFiles(movie_poster_path, "*.jpg"));
foreach (String filename in filenames) {
this.movie_posters_list.Add(new BitmapImage(new Uri(filename)));
Console.WriteLine("filename " + filename);
Match regex_match = Regex.Match(filename.Trim(), regex_pattern);
String matched_movie_name = regex_match.Groups[1].Value;
this.movie_names.Add(matched_movie_name);
Console.WriteLine("Movie Name: " + matched_movie_name);
}
MovieListView.ItemsSource = movie_posters_list;
}
}
}
Edit: I tried the method outlined by #XAML Lover but I don't get the images appearing at all anymore. I'm not sure whether this is a data binding issue.
The TextBlock is hiding the whole ListView, which is blocking the user input. Look at the modified XAML,
<Grid x:Name="movie_grid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<ListView Grid.Row="1"
Name="MovieListView"
ItemTemplate="{StaticResource ItemTemplate}"
ItemsSource="{Binding Path = movie_posters_list}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="5" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<TextBlock Name="SampleTextBlock"
Text="{Binding Path=movie_names}"
DataContext="{StaticResource ItemTemplate}" />
</Grid>
Put your xaml of displaying the pictures in a grid then put that grid inside scrollviewer control and set the desired orientation and alignments and you will get the solution.
<ScrollViewer orientation="" VerticleAllignment="" HorizontalAllignment="">
<Grid>
Place your xaml here...
</Grid>
</ScrollViewer>
I Work with the theme of MahApps (Metro Dark) I looked the animations of this theme.
I came to a dead end: indeed I created a system to switch between different UserControl, that is to say that I have only one window and clicking on different buttons, I have this or such UserControl. But now I am with this system switch, I have no animation (only the start of the application).
How can I make an animation for each change in UserControl (Keeping Metro theme)?
Somebody ask me : use TransitioningContentControl
But i made my switcher like this :
class Switcher
{
public static UserControl WClient;
public static UserControl WHome;
public static UserControl WDataBase;
public Switcher()
{
WClient = new Windows.Client();
WHome = new Windows.Home();
WDataBase = new Windows.DataBase();
}
public static void currentWindow(UserControl window, string color)
{
Window curApp = Application.Current.MainWindow;
curApp.Content = window;
if (window == WClient)
{
curApp.Title = "CLIENT - INFO-TOOLS - BY NAOGRAFIX";
}
else if (window == WDataBase)
{
curApp.Title = "DATABASE - INFO-TOOLS - BY NAOGRAFIX";
}
else
{
curApp.Title = "HOME - INFO-TOOLS - BY NAOGRAFIX";
}
currentColor(color);
}
}
Now, when i clic on a button (to switch userControl) i use this :
private void BtnDataBase_Click(object sender, RoutedEventArgs e)
{
var color = "Red";
if (DataBase.isConnected) { color = "Green"; }
Switcher.currentWindow(Switcher.WDataBase, color);
}
I use CONTENT, i dont know if i can use TransitioningContentControl
Help :)
Nao*
You do need to use transitioning content control as you have said. You can add that as the direct content of the window then access it by name from the mainwindow and change its content instead.
Xaml
<metro:TransitioningContentControl x:Name="tContent"/>
C#
((ContentControl)curApp.FindName("tContent")).Content = window;
You will need the xml namespace definition
xmlns:metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
and you can change the transition using the Transition property on TransitioningContentControl
WPF XAML below shows use of MahApps.Metro TransitioningContentControl.
Click on the Content listbox to switch content.
Select the transition effect in the Transition listbox, then change selected Content to see the effect.
<Window x:Class="WpfMahApp.MainWindow"
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"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800">
<Window.Resources>
<TextBlock x:Key="Content1" Width="400" Height="200" Text="Content 1: TextBox" Background="Aqua" />
<Canvas x:Key="Content2" Width="200" Height="400" Background="DarkOrange">
<Ellipse Fill="YellowGreen" Stroke="Black" Width="100" Height="200" />
<Label Content="Content2: Canvas" />
</Canvas>
<Border x:Key="Content3" Width="100" Height="100" Background="Yellow" BorderBrush="Blue" BorderThickness="2" CornerRadius="4">
<TextBlock Text="Content3: Border" />
</Border>
</Window.Resources>
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical" >
<CheckBox Margin="4" Content="Is Transitioning" IsChecked="{Binding ElementName=TransitioningContentControl,Path=IsTransitioning , Mode=OneWay}" />
<StackPanel Orientation="Vertical" Margin="8">
<TextBlock Text="Content" FontWeight="Bold"/>
<ListBox Name="ContentSelection" HorizontalAlignment="Left">
<ListBoxItem Content="Content 1" Tag="{StaticResource Content1}" />
<ListBoxItem Content="Content 2" Tag="{StaticResource Content2}" />
<ListBoxItem Content="Content 3" Tag="{StaticResource Content3}" />
</ListBox>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="8">
<TextBlock Text="Transition" FontWeight="Bold" />
<ListBox Name="Transition" HorizontalAlignment="Left">
<ListBoxItem Content="Default" Tag="{x:Static mah:TransitionType.Default}"/>
<ListBoxItem Content="Normal" Tag="{x:Static mah:TransitionType.Normal}"/>
<ListBoxItem Content="Up" Tag="{x:Static mah:TransitionType.Up}"/>
<ListBoxItem Content="Down" Tag="{x:Static mah:TransitionType.Down}"/>
<ListBoxItem Content="Left" Tag="{x:Static mah:TransitionType.Left}" />
<ListBoxItem Content="Right" Tag="{x:Static mah:TransitionType.Right}"/>
<ListBoxItem Content="LeftReplace" Tag="{x:Static mah:TransitionType.LeftReplace}"/>
<ListBoxItem Content="RightReplace" Tag="{x:Static mah:TransitionType.RightReplace}"/>
</ListBox>
</StackPanel>
</StackPanel>
<mah:TransitioningContentControl Margin="8"
Name="TransitioningContentControl"
Background="Beige" BorderBrush="Black" BorderThickness="1"
Content="{Binding ElementName=ContentSelection, Path=SelectedValue.Tag}"
Transition="{Binding ElementName=Transition, Path=SelectedValue.Tag}" />
</StackPanel>
</Window>
I'm doing some drag and drop between two ListBox items containing a UserControl, ClaimSectionTemplate as the DataTemplate for the items in the collection that are populating the source listbox.
Now I have 2 buttons on ClaimSectionTemplate, AddField and RemoveField, and they respectively add and remove fields from a child collection on the ClaimSection object that is shown in the ClaimSectionTemplate user control.
So what is happening is when I drop a ClaimSection into the target ListBox the original object becomes unresponsive no longer allowing me to interact with the user control.
MainWindow.Xaml
<ListBox Margin="13,12,12,12" Name="NewSections" Grid.Column="1" AllowDrop="True" Drop="NewSections_Drop">
<ListBox.ItemTemplate>
<DataTemplate>
<me:ClaimSectionTemplate />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Main Window Drag Drop Handlers
private void ExistingSections_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var parent = (ListBox)sender;
dragSource = parent;
var data = ExistingSections.SelectedItem;
if (data != null)
{
DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
}
}
private void NewSections_Drop(object sender, DragEventArgs e)
{
Models.ClaimSection dropData = (Models.ClaimSection)e.Data.GetData(typeof(Models.ClaimSection));
ClaimSectionsNew.addClaimSection(dropData);
ClaimSectionsExisting.removeClaimSection(dropData);
}
ClaimSectionTemplate.xaml
<UserControl x:Class="InsuranceBuildVer1.Views.ClaimSectionTemplate"
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="187" d:DesignWidth="300">
<Grid Height="185">
<TextBlock Height="23" Margin="12,12,12,0" Name="textBlock1" Text="{Binding Path=ClaimType}" VerticalAlignment="Top" />
<ListBox x:Name="FieldList" HorizontalAlignment="Left" Margin="10,71,0,12" Width="278" ItemsSource="{Binding Path=Fields.ClaimFields, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Identifier}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Add Field" Height="23" HorizontalAlignment="Left" Margin="12,42,0,0" Name="AddField" VerticalAlignment="Top" Width="75" Click="AddField_Click" />
<Button Content="Remove Field" Height="23" HorizontalAlignment="Left" Margin="103,42,0,0" Name="RemoveField" VerticalAlignment="Top" Width="96" Click="RemoveField_Click" />
</Grid>
</UserControl>