I need to create 10 or more text boxes that have the same event and logic,
instead of copy and paste it there is option to create it once and than inherit from it?
For example name1 will inherit from name
<TextBox x:Name="Name" AcceptsReturn="True" AllowDrop="True"
PreviewDragEnter="DropText_PreviewDragEnter"
PreviewDrop="DropText_PreviewDrop"
SelectionChanged="listbox_SelectionChanged"
HorizontalAlignment="Left" Height="25" TextWrapping="Wrap"
VerticalAlignment="Top" Width="150" Grid.Column="4" Margin="0,50,0,0"
Grid.Row="2" />
<TextBox x:Name="name1" AcceptsReturn="True" AllowDrop="True"
PreviewDragEnter="DropText_PreviewDragEnter"
PreviewDrop="DropText_PreviewDrop"
SelectionChanged="listbox_SelectionChanged"
HorizontalAlignment="Left" Height="25" TextWrapping="Wrap"
VerticalAlignment="Top" Width="150" Grid.Column="4" Margin="0,50,0,0"
Grid.Row="2" />
you can create a control template of your textbox as a resource. Mark the resource as x:Shared=False
<Grid.Resources>
<ControlTemplate TargetType="TextBox" x:Key="TxtBoxTemplate" x:Shared="False">
<Grid.Resources>
use this template on other instances of textbox.
<TextBox x:Name="name1" Grid.Row="0" Template="{StaticResource TxtBoxTemplate}"/>
<TextBox x:Name="name2" Grid.Row="1" Template="{StaticResource TxtBoxTemplate}"/>
<TextBox x:Name="name3" Grid.Row="2" Template="{StaticResource TxtBoxTemplate}"/>
Complete code as follows.
<Grid>
<Grid.Resources>
<ControlTemplate TargetType="TextBox" x:Key="TxtBoxTemplate" x:Shared="False">
<TextBox x:Name="Name"
AcceptsReturn="True"
AllowDrop="True"
PreviewDragEnter="DropText_PreviewDragEnter"
PreviewDrop="DropText_PreviewDrop"
SelectionChanged="listbox_SelectionChanged"
HorizontalAlignment="Left" Height="25" TextWrapping="Wrap"
VerticalAlignment="Top" Width="150" Grid.Column="4" Margin="0,50,0,0" Grid.Row="2"
TextChanged="Name_OnTextChanged"
/>
</ControlTemplate>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox x:Name="name1" Grid.Row="0" Template="{StaticResource TxtBoxTemplate}"/>
<TextBox x:Name="name2" Grid.Row="1" Template="{StaticResource TxtBoxTemplate}"/>
<TextBox x:Name="name3" Grid.Row="2" Template="{StaticResource TxtBoxTemplate}"/>
</Grid>
As mentioned by HighCore in comment, ItemsControl might be of your help here.
Create an ItemsControl and set ItemTemplate to have TextBox inside it. (Create ObjectDataProvider to return number of textBoxes you need)
<Grid>
<Grid.Resources>
<ObjectDataProvider x:Key="EnumerableRange"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:linq="clr-namespace:System.Linq;assembly=System.Core"
ObjectType="{x:Type linq:Enumerable}" MethodName="Range">
<ObjectDataProvider.MethodParameters>
<sys:Int32>1</sys:Int32>
<sys:Int32>15</sys:Int32>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Grid.Resources>
<ItemsControl ItemsSource="{Binding Source={StaticResource EnumerableRange}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox x:Name="Name"
AcceptsReturn="True"
AllowDrop="True"
PreviewDragEnter="DropText_PreviewDragEnter"
PreviewDrop="DropText_PreviewDrop"
SelectionChanged="listbox_SelectionChanged"
HorizontalAlignment="Left" Height="25"
TextWrapping="Wrap"
VerticalAlignment="Top" Width="150" Grid.Column="4"
Margin="0,50,0,0" Grid.Row="2"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
UPDATE
You can use Style then to declare common properties over there and events and simply have multiple instances of TextBox referring to that style.
<Grid>
<Grid.Resources>
<Style TargetType="TextBox">
<Setter Property="AcceptsReturn" Value="True"/>
<Setter Property="AllowDrop" Value="True"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Width" Value="150"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Margin" Value="0,50,0,0"/>
<EventSetter Event="PreviewDragEnter"
Handler="DropText_PreviewDragEnter"/>
<EventSetter Event="PreviewDrop"
Handler="DropText_PreviewDrop"/>
<EventSetter Event="SelectionChanged"
Handler="listbox_SelectionChanged"/>
</Style>
</Grid.Resources>
<TextBox x:Name="Name"/>
<TextBox x:Name="Name1"/>
......
<TextBox x:Name="Name15"/>
</Grid>
Note i haven't set x:Key on Style so it will get applied by default to all TextBoxes in your Grid. If you don't want that set x:Key and use that for all TextBoxes.
# first method : create a class with name customTextbox.cs and write down properties and events for textbox
public class customTextbox:TextBox
{
public customTextbox():base()
{
this.AcceptsReturn = true;
this.AllowDrop = true;
this.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
//set your remaining propreties
this.PreviewDragEnter +=customTextbox_PreviewDragEnter;
this.PreviewDragLeave +=customTextbox_PreviewDragLeave;
this.SelectionChanged += customTextbox_SelectionChanged;
}
void customTextbox_SelectionChanged(object sender, System.Windows.RoutedEventArgs e)
{
//code here
}
private void customTextbox_PreviewDragLeave(object sender, System.Windows.DragEventArgs e)
{
//code here
}
private void customTextbox_PreviewDragEnter(object sender, System.Windows.DragEventArgs e)
{
//code here
}
}
and in xaml
<Window x:Class="WpfApplication3.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"
xmlns:hp="clr-namespace:WpfApplication3">
<Grid Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<hp:customTextbox Height="100" Width="100" Grid.Row="0"></hp:customTextbox>
<hp:customTextbox Grid.Row="1" ></hp:customTextbox>
<hp:customTextbox Grid.Row="2" ></hp:customTextbox>
</Grid>
#second method:create a usecontrol like this(http://prntscr.com/2mren4) and replace usercontrol with textbox like below
<TextBox x:Class="WpfApplication3.CustomTB"
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="300" d:DesignWidth="300">
<!--set all properties and event you required inside textbox-->
and in .cs file use textbox instead of usercontrol like below
public partial class CustomTB : TextBox
{
public CustomTB()
{
InitializeComponent();
}
}
and in another window add
<Window x:Class="WpfApplication3.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"
xmlns:hp="clr-namespace:WpfApplication3">
<Grid Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<hp:CustomTB Grid.Row="0"/>
</Grid>
Related
Current custom window(base) is:
<Window x:Class="Views.DialogWindow"
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:Views"
xmlns:dialog="clr-namespace:ViewModels"
mc:Ignorable="d"
Title="DialogWindow" Height="450" Width="800" WindowStartupLocation="CenterScreen" WindowStyle="None" AllowsTransparency="True" Background="Transparent" ResizeMode="CanResizeWithGrip" SizeToContent="WidthAndHeight">
<Window.Resources>
<ResourceDictionary>
<DataTemplate DataType="{x:Type dialog:Dialog_1ViewModel}">
<local:Dialog_1></local:Dialog_1>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
<ContentPresenter x:Name="ContentPresenter" Content="{Binding}"></ContentPresenter>
</Window>
Window in designer:
Its DataContext is set to an instance of UserControl:
<UserControl x:Class="Views.Dialog_1"
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:Views"
xmlns:t1="clr-namespace:Windows"
mc:Ignorable="d" d:DesignHeight="400" d:DesignWidth="600">
<UserControl.Resources>
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="TextTrimming" Value="CharacterEllipsis"></Setter>
</Style>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Resource_1.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Border CornerRadius="4" Background="#ffff" BorderThickness="1" BorderBrush="#9888">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="12*"/>
<RowDefinition Height="15*"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<Border Grid.Row="0" BorderThickness="0,0,0,1" BorderBrush="#f999" Cursor="Hand" MouseDown="Border_MouseDown">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="Header"></TextBlock>
</Border>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" MinWidth="150"></ColumnDefinition>
<ColumnDefinition Width="3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Margin="4">
<Image Source="C:\test.png"></Image>
</Grid>
<Grid Grid.Column="1" Margin="4">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<DockPanel>
<TextBlock Text="Foo: " FontWeight="Bold" Margin="0,0,4,0"></TextBlock>
<TextBlock Text="Bar"></TextBlock>
</DockPanel>
<DockPanel Margin="0,8,0,0">
<TextBlock Text="Foo: " FontWeight="Bold" Margin="0,0,4,0"></TextBlock>
<TextBlock Text="Bar"></TextBlock>
</DockPanel>
<DockPanel>
<TextBlock Text="Foo: " FontWeight="Bold" Margin="0,0,4,0"></TextBlock>
<TextBlock Text="Bar"></TextBlock>
</DockPanel>
<DockPanel>
<TextBlock Text="Foo: " FontWeight="Bold" Margin="0,0,4,0"></TextBlock>
<TextBlock Text="Bar"></TextBlock>
</DockPanel>
</StackPanel>
<DockPanel Grid.Row="1" VerticalAlignment="Center">
<TextBlock Text="Foo: " FontWeight="Bold" Margin="0,0,4,0"/>
<TextBlock Text="Bar"/>
</DockPanel>
</Grid>
</Grid>
<DataGrid Grid.Row="2" Background="#ff88" ColumnWidth="Auto" VerticalAlignment="Stretch" AutoGenerateColumns="False" ItemsSource="{Binding data_1}">
<DataGridTextColumn Header="Name" MinWidth="200" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Description" MinWidth="100" Binding="{Binding Description}"/>
</DataGrid>
<Border Grid.Row="3" BorderThickness="0,1,0,0" BorderBrush="#f999">
<Grid HorizontalAlignment="Right" Margin="4">
<Button Style="{StaticResource T_button_1}" HorizontalAlignment="Right" VerticalAlignment="Center" Width="100" Command="{Binding C_No}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}" t1:Button_1.Icon="/Icons/no.png" t1:Button_1.Label="Cancel"/>
<Button Style="{StaticResource T_button_1}" HorizontalAlignment="Right" VerticalAlignment="Center" Width="100" Command="{Binding C_Yes}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}" Margin="0,0,105,0"/>
</Grid>
</Border>
</Grid>
</Border>
</UserControl>
UserControl in designer:
The result(run and resize process):
Close look shows interesting behaviour(seems like style changes):
Edit 1
Made some more minimized version:
<UserControl x:Class="Views.Dialog_1"
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="400" d:DesignWidth="600">
<UserControl.Resources>
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="TextTrimming" Value="CharacterEllipsis"></Setter>
</Style>
<Style TargetType="Grid">
<Setter Property="ShowGridLines" Value="True"></Setter>
</Style>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Resource_1.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Border Background="#ffff" BorderThickness="1" BorderBrush="#ccc">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"></RowDefinition>
<RowDefinition Height="2*"/>
<RowDefinition Height="3*"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<Border Grid.Row="0">
</Border>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Margin="4">
<Image Source="C:\test.ico"></Image>
</Grid>
<Grid Grid.Column="1" Margin="4">
</Grid>
</Grid>
<Grid Grid.Row="2">
</Grid>
<Border Grid.Row="3">
</Border>
</Grid>
</Border>
</UserControl>
When run:
What could cause such issue where controls' initial sizes are oddly wrong and after resizing they return to "normal" or how would it be possible to fix/escape that situation leaving similar logic with custom UserControl of Window?
May this be caused by ContentPresenter part?
I'm creating an application for educational purposes. How can i use multiple User controls in my "MainWindow.xaml"?
I want to use User controls on my MainWindow so that I wont need multiple windows. So after you press next on the sign up layout,
it should take you to the thank you screen which is also another UserControl class. Although in the same Window.
I've read as many different "solutions" as I could, without any real luck..
Here's the code I have atm.
Main Window.xaml
<Window x:Class="WpfApp1.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:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="700" Width="700"
WindowStartupLocation="CenterScreen">
<Grid>
<!--Background image-->
<Grid.Background >
<ImageBrush ImageSource="login-page-background-3.jpg"/>
</Grid.Background>
<!--Main content scroll-->
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<local:SignUpControl>
</local:SignUpControl>
</ScrollViewer>
</Grid>
MainWindow.xaml.cs has no code...
SignUpControl.Xaml
<UserControl x:Class="WpfApp1.SignUpControl"
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:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<StackPanel
VerticalAlignment="Center"
HorizontalAlignment="Center"
TextBlock.TextAlignment="Center">
<!--Login main content white box-->
<Border Background="WhiteSmoke"
Opacity="0.4"
CornerRadius="30"
Padding="15 50 15 15"
Width="350"
Margin="50 50 50 0">
<StackPanel>
<!--Sign Up header-->
<TextBlock FontSize="20"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Height="auto"
FontFamily="Goudy Stout" >Sign Up</TextBlock>
<!--Sign up subtext-->
<TextBlock FontSize="14"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Height="auto"
FontFamily="Ravie" >It's about to get fun!</TextBlock>
<!--Inner grid for Username & Password-->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!--Textbox for username-->
<TextBox Grid.Row="0" BorderThickness="0" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontFamily="Arial" FontWeight="Bold" FontSize="14" x:Name="UsernameBox" Margin="5"/>
<TextBlock IsHitTestVisible="False" Text="Username" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10,0,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="14" Foreground="Black">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Text, ElementName=UsernameBox}" Value="">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<!--PasswordBox-->
<TextBox Grid.Row="1" BorderThickness="0" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontFamily="Arial" FontWeight="Bold" FontSize="14" x:Name="passwordBox" Margin="5"/>
<TextBlock Grid.Row="1" IsHitTestVisible="False" Text="password" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10,0,0,0" FontFamily="Arial" FontWeight="Bold" FontSize="14" Foreground="Black">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Text, ElementName=passwordBox}" Value="">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
<!--Next Button-->
<Button Content="Next"
HorizontalAlignment="Center"
FontWeight="Bold"
FontSize="14"
BorderThickness="0"
FontFamily="Goudy Stout"
Background="Transparent"
Padding="20 10"
Margin="0 10"
x:Name="NextButton"
Click="NextButton_Click"/>
</StackPanel>
</Border>
<!--Border for PreRegistered account-->
<Border Background="WhiteSmoke"
Opacity="0.4"
CornerRadius="50"
Padding="0"
Width="400"
Height="auto"
Margin="0 12.5 0 0">
<!--Already registered button-->
<Button Content="I already have an account"
HorizontalAlignment="Center"
Opacity="0.8"
FontSize="13"
BorderThickness="0"
FontFamily="Goudy Stout"
Background="Transparent"
Foreground="Black"
x:Name="alreadyRegBtn"
Padding="0 10"
Margin="0 5 0 5"/>
</Border>
</StackPanel>
How do I go about doing this solution where I can change between user controls on the same window, Ofcourse after the thank you screen I will be using the same logic to go to "Sign In!" an so on...
When i'm not using Prism or Dependency Injection then I normally use the View Model first approach.
In this scenario we have a property on our Windows ViewModel that is a class that the other UserControls ViewModels inherit from, normally just use a ViewModelBase class that has the implimentation of INotifyPropertyChanged:
private ViewModelBase currentViewModel;
public ViewModelBase CurrentViewModel
{
get { return currentViewModel; }
set { currentViewModel = value; NotifyPropertyChanged(); }
}
Now inside of your MainWindow like #Tomtom said you have a ContentControl bound to that property. This means that using DataTemplates You can have a different View display depending on which ViewModel type is currently on that property.
<Window.Resources>
<DataTemplate DataType="{x:Type viewmodels:ViewModel1}">
<views:View1/>
</DataTemplate>
<DataTemplate DataType="{x:Type viewmodels:ViewModel2}">
<views:View2/>
</DataTemplate>
</Window.Resources>
<ContentControl Content="{Binding CurrentViewModel}"/>
With this in place all you need to do is have some logic that changes the ViewModel on the MainWindow ViewModel and the View will update to display the correct View for that ViewModel.
Edit: One thing to note is there are lots of different ways people use to achieve what you want, none of them are really right or wrong but they all suit different peoples needs and coding styles.
I've answered a similar question a while ago.
See this post
You can create a ContentControl in your Window and switch the bound UserControl where the user clicks or something else
I've got an issue trying catching events on item from a ListView.
WPF Sample
<ListView x:Name="itemsListView" IsHoldingEnabled="True"
ItemContainerStyle="{StaticResource SimpleListItem}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="ItemSp" Holding="ItemSp_Holding">
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="Modifier" />
<MenuFlyoutItem Text="Supprimer" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<TextBlock Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code Behind Sample
private void ItemSp_Holding(object sender, HoldingRoutedEventArgs e)
{
Debug.WriteLine("holded");
}
Style Sample
<Style x:Key="SimpleListItem" TargetType="ListViewItem">
<Setter Property="BorderThickness" Value="0 0 0 2"></Setter>
<Setter Property="BorderBrush" Value="{StaticResource FlashGreen}"></Setter>
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
<Setter Property="Background" Value="{StaticResource SmoothDark}"></Setter>
<Setter Property="FontSize" Value="24"></Setter>
</Style>
Events aren't fired on Item (Tap or Hold, same problem).
Outside of these ListView events are working.
Am I doing something wrong?
EDIT:
Full WPF UC
<UserControl
x:Class="MuchroomPhone.ShopItemsUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MuchroomPhone"
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">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="9*"></RowDefinition>
<RowDefinition Height="1*"></RowDefinition>
</Grid.RowDefinitions>
<Grid x:Name="ItemsGrid">
<ListView x:Name="itemsListView" IsHoldingEnabled="True" ItemContainerStyle="{StaticResource SimpleListItem}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="ItemSp" Holding="ItemSp_Holding">
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="Modifier" />
<MenuFlyoutItem Text="Supprimer" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<TextBlock Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
<Grid x:Name="NoItemsGrid">
<TextBlock Style="{StaticResource EmptyElemTextBox}">Aucun item</TextBlock>
</Grid>
<Grid x:Name="AddItemModal" Background="#AA000000" Canvas.ZIndex="1" Visibility="Collapsed">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Rectangle Grid.Row="1" Fill="White"></Rectangle>
<TextBox x:Name="NewItemTb" VerticalAlignment="Center" Grid.Row="1" PlaceholderText="Nom de l'item" ></TextBox>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<Button x:Name="CancelAddItemBtn" Click="CancelAddItemBtn_Click">Annuler</Button>
<Button x:Name="AddItemBtn" Click="AddItemBtn_Click">Ajouter</Button>
</StackPanel>
</Grid>
<Rectangle Grid.Row="2" Fill="Black"></Rectangle>
<Button Grid.Row="2" Click="addItem_Click">New Item</Button>
</Grid>
</UserControl>
working on nearly the same subject just now.. templated listviewitem inside templated usercontrol with handling listview events outside of usercontrol
code below will give me item_holding event outside usercontrol
inside loaded event
lv = Listview
//setting event handler to items in the view
private void ConnectListView( ListView lv , SelectionChangedEventHandler SelectionChanged, HoldingEventHandler Item_holding)
{
lv.SelectionChanged += SelectionChanged;
for (int i = 0; i < lv.Items.Count; i++)
{
ListViewItem item = (ListViewItem)lv.ContainerFromIndex(i);
if (item != null)item.Holding += Item_holding;
}
}
// for items outside view in Listview ContainerContentChanging event
private void LbContacts_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
args.ItemContainer.Holding -= lbContactsItem_Holding;
args.ItemContainer.Holding += lbContactsItem_Holding;
}
still playing around and testing, but it seems that it is working.
don't like to do this in ContainerContentChanging event because it is calling too frequently, but for now don't find another point.
I have a TextBlock that dislays a message. I have to build the ability to edit and save this message. When the Edit Icon is clicked, instead of popping up another view with a TextBox and a Save Button, I wanted to somehow just show a Textbox and Save Button in place of the TextBlock. Then when Save is clicked it would change the State back again only showing the read only TextBlock.
I thought that I could do this with the VisualStateManager but it seems you can only do Storyboards for States and even if I leave the button always there and make it invisible I can't seem to change the Property Value between States either.
Is VisualStateManager the answer for this or is there something better to use?
Update
I found and followed this example View / Edit Control and it looks like it will solve my issue.
<UserControl x:Class="WpfApplication1.Test2"
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:c="clr-namespace:WpfApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Name="MyTest" c:ReadOnlyControlTemplate.Enabled="True" c:ReadOnlyControlTemplate.DoLock="False" Grid.Column="0" Grid.Row="0" Content="{Binding Path=Message}" >
<Control.Template>
<ControlTemplate TargetType="{x:Type Label}">
<TextBox Width="{TemplateBinding Property=Width}" Height="{TemplateBinding Property=Height}" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" AcceptsReturn="True" Margin="100,0" />
</ControlTemplate>
</Control.Template>
<c:ReadOnlyControlTemplate.LockTemplate>
<ControlTemplate TargetType="{x:Type Label}">
<Border BorderBrush="Red" BorderThickness="3">
<TextBlock Width="{TemplateBinding Property=Width}" Height="{TemplateBinding Property=Height}" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" Foreground="White" />
</Border>
</ControlTemplate>
</c:ReadOnlyControlTemplate.LockTemplate>
</Label>
<WrapPanel Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center">
<Button Content="ReadOnly" Width="70" Height="25" Click="Button_Click" Margin="0,0,5,0" />
<Button Content="Edit" Grid.Column="0" Grid.Row="1" Width="50" Height="25" Click="Button_Click2" />
</WrapPanel>
</Grid>
I have this strange problem with AppBarButton. I have binded the button to a method in my view model like this:
XAML for MainView
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" BackgroundColor="{StaticResource HsAppBarColor}" Opacity="1">
<shell:ApplicationBar.Buttons>
<cal:AppBarButton IconUri="/icons/appbar.feature.settings.rest.png" Text="ajustes" Message="GoToSettings" />
<cal:AppBarButton IconUri="/icons/appbar.favs.rest.png" Text="favoritos" Message="GoToFavs" />
</shell:ApplicationBar.Buttons>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
C# for MainViewModel
public void GoToSettings() {
navigation.UriFor<SettingsViewModel>().Navigate();
}
It fires GoToSettings code and it navigates to SettingsView page however, SettingsViewModel is never initialized and the constructor is not called. Caliburn code for binding view with view model doesn't work.
I wonder if having a Pivot in MainPage which sets a DataContext for its Sample Data could affect. I tried commenting the data context but it still didn't called SettingsViewModel constructor.
This is the Pivot XAML code
<controls:Pivot toolkit:TurnstileFeatherEffect.FeatheringIndex="0" Margin="0" Title="" DataContext="{Binding Source={StaticResource Stores}}" TitleTemplate="{StaticResource StoresPivotTitleTemplate}" HeaderTemplate="{StaticResource StoresPivotHeaderTemplate}" Style="{StaticResource StoresPivotStyle}" FontFamily="Arial Narrow" Background="{x:Null}">
<controls:PivotItem Header="Restorantes" BorderThickness="0,-20,0,0" Margin="12,0" Background="{x:Null}">
<Grid>
<ListBox toolkit:TurnstileFeatherEffect.FeatheringIndex="2" cal:Message.Attach="[Event SelectionChanged] = [Action Restaurants_SelectionChanged];" ItemTemplate="{StaticResource StoresListItemTemplate}" ItemsSource="{Binding Collection}" Margin="0" ItemContainerStyle="{StaticResource StoresListContainerStyle}" Background="{x:Null}"/>
</Grid>
</controls:PivotItem>
<controls:PivotItem Header="Tiendas" Margin="12,0">
<Grid>
<ListBox toolkit:TurnstileFeatherEffect.FeatheringIndex="2" cal:Message.Attach="[Event SelectionChanged] = [Action Stores_SelectionChanged];" ItemTemplate="{StaticResource StoresListItemTemplate}" ItemsSource="{Binding Collection}" Margin="0" ItemContainerStyle="{StaticResource StoresListContainerStyle}" FontFamily="Segoe WP"/>
</Grid>
</controls:PivotItem>
</controls:Pivot>
Stores_SelectionChanged method also navigates to a page but this time the binding works and the viewmodel constructor is fired.
Any idea why it doesn't work in the AppBarButton but it does work in the Pivot attached action when they use the same view model behind?
EDIT: added full xaml for view
<phone:PhoneApplicationPage
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
xmlns:controlsPrimitives="clr-namespace:Microsoft.Phone.Controls.Primitives;assembly=Microsoft.Phone.Controls"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
x:Class="Happyshop.Views.StoresView"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True" Foreground="{x:Null}">
<toolkit:TransitionService.NavigationInTransition>
<toolkit:NavigationInTransition>
<toolkit:NavigationInTransition.Backward>
<toolkit:TurnstileFeatherTransition Mode="BackwardIn"/>
</toolkit:NavigationInTransition.Backward>
<toolkit:NavigationInTransition.Forward>
<toolkit:TurnstileFeatherTransition Mode="ForwardIn" BeginTime="0:0:0.7"/>
</toolkit:NavigationInTransition.Forward>
</toolkit:NavigationInTransition>
</toolkit:TransitionService.NavigationInTransition>
<toolkit:TransitionService.NavigationOutTransition>
<toolkit:NavigationOutTransition>
<toolkit:NavigationOutTransition.Backward>
<toolkit:TurnstileFeatherTransition Mode="BackwardOut"/>
</toolkit:NavigationOutTransition.Backward>
<toolkit:NavigationOutTransition.Forward>
<toolkit:TurnstileFeatherTransition Mode="ForwardOut" BeginTime="0:0:0.2"/>
</toolkit:NavigationOutTransition.Forward>
</toolkit:NavigationOutTransition>
</toolkit:TransitionService.NavigationOutTransition>
<phone:PhoneApplicationPage.Resources>
<Style x:Key="StoresPivotStyle" TargetType="controls:Pivot">
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:Pivot">
<Grid HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
<Grid.Background>
<SolidColorBrush Color="{StaticResource HsDarkBlue}"/>
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="65"/>
<RowDefinition Height="96"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid CacheMode="BitmapCache" Grid.RowSpan="3"/>
<Border Margin="-12,-5,0,0" BorderThickness="0,0,0,0">
<ContentPresenter ContentTemplate="{TemplateBinding TitleTemplate}" Content="{TemplateBinding Title}" Margin="12,5,0,0"/>
</Border>
<Border Grid.Row="1" toolkit:TurnstileFeatherEffect.FeatheringIndex="1" >
<controlsPrimitives:PivotHeadersControl x:Name="HeadersListElement" Background="Black"/>
</Border>
<ItemsPresenter x:Name="PivotItemPresenter" Margin="0" Grid.Row="2"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</phone:PhoneApplicationPage.Resources>
<phone:PhoneApplicationPage.Background>
<SolidColorBrush Color="{StaticResource HsDarkBlue}"/>
</phone:PhoneApplicationPage.Background>
<phone:PhoneApplicationPage.FontFamily>
<StaticResource ResourceKey="PhoneFontFamilyNormal"/>
</phone:PhoneApplicationPage.FontFamily>
<phone:PhoneApplicationPage.FontSize>
<StaticResource ResourceKey="PhoneFontSizeNormal"/>
</phone:PhoneApplicationPage.FontSize>
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" BackgroundColor="{StaticResource HsAppBarColor}" Opacity="1">
<shell:ApplicationBar.Buttons>
<cal:AppBarButton IconUri="/icons/appbar.feature.settings.rest.png" Text="ajustes" Message="GoToSettings" />
<cal:AppBarButton IconUri="/icons/appbar.favs.rest.png" Text="favoritos" Message="GoToFavs" />
</shell:ApplicationBar.Buttons>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
<controls:Pivot toolkit:TurnstileFeatherEffect.FeatheringIndex="0" Margin="0" Title="" DataContext="{Binding Source={StaticResource Stores}}" TitleTemplate="{StaticResource StoresPivotTitleTemplate}" HeaderTemplate="{StaticResource StoresPivotHeaderTemplate}" Style="{StaticResource StoresPivotStyle}" FontFamily="Arial Narrow" Background="{x:Null}">
<controls:PivotItem Header="Restorantes" BorderThickness="0,-20,0,0" Margin="12,0" Background="{x:Null}">
<Grid>
<ListBox toolkit:TurnstileFeatherEffect.FeatheringIndex="2" cal:Message.Attach="[Event SelectionChanged] = [Action Restaurants_SelectionChanged];" ItemTemplate="{StaticResource StoresListItemTemplate}" ItemsSource="{Binding Collection}" Margin="0" ItemContainerStyle="{StaticResource StoresListContainerStyle}" Background="{x:Null}"/>
</Grid>
</controls:PivotItem>
<controls:PivotItem Header="Tiendas" Margin="12,0">
<Grid>
<ListBox toolkit:TurnstileFeatherEffect.FeatheringIndex="2" cal:Message.Attach="[Event SelectionChanged] = [Action Stores_SelectionChanged];" ItemTemplate="{StaticResource StoresListItemTemplate}" ItemsSource="{Binding Collection}" Margin="0" ItemContainerStyle="{StaticResource StoresListContainerStyle}" FontFamily="Segoe WP"/>
</Grid>
</controls:PivotItem>
</controls:Pivot>
UPDATE
It was a stupid error. When you use generate class with intellisense the class created is not marked as public. Classes are by default marked as internal so Caliburn project could not access the ViewModel and hence why it would not bind it.
Resolved. The problem was that classes created via code generation of Intellisense in Visual Studio are not marked as public.
Classes must be marked public in order to Caliburn to be able to access and instantiate one. Otherwise they use the default visibility which is internal