I am trying to make CarouselPage in my Xamarin Project.
<?xml version="1.0" encoding="utf-8" ?>
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ABP.Views.ProcessSurveysPage">
<ContentPage>
<StackLayout>
<Label Text="Red" />
<BoxView Color="Red" VerticalOptions="FillAndExpand" />
</StackLayout>
</ContentPage>
<ContentPage>
<StackLayout>
<Label Text="Green" />
<BoxView Color="Green" VerticalOptions="FillAndExpand" />
</StackLayout>
</ContentPage>
<ContentPage>
<StackLayout>
<Label Text="Blue" />
<BoxView Color="Blue" VerticalOptions="FillAndExpand" />
</StackLayout>
</ContentPage>
</CarouselPage>
Here is xaml.cs file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace ABP.Views
{
public partial class ProcessSurveysPage : CarouselPage
{
public ProcessSurveysPage()
{
InitializeComponent();
this.Title = "Current Bookings";
}
}
}
This is caller.
private void GotoProcessSurveysPage(object sender, EventArgs args)
{
Device.BeginInvokeOnMainThread(() => Navigation.PushAsync(new ProcessSurveysPage()));
//await this.Navigation.PushAsync(new DateSearchFilterView());
}
I tested on Android, Window 8.
But while I deployed it on my window phone 8.1 and swiped on pages but any action has not happened.
How can I fix this?
What's my wrong?
Related
I have a BasePage created using ControlTemplates that contains a loading overlay for each child to use - This overlay has a "cancel" button on it, but for some reason I can't get ICommands to execute when I tap the buttons. Clicked events work fine but I'd like to understand what the problem is with Commands.
I researched the issue and found I should be binding using Command="{TemplateBinding MyCommand}" since my content is within a ControlTemplate but still no luck, however I am also binding the Text property and this is working fine, so I'm a bit confused.
Here's a cut down version of what I've hacked together.
Here's my BasePage XAML with the button in question:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage x:Name="this" NavigationPage.HasNavigationBar="False" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:helpers="clr-namespace:ShoppingListNEW.MarkupExtensions" xmlns:views="clr-namespace:ShoppingListNEW.Views" x:Class="ShoppingListNEW.Pages.BasePage">
<ContentPage.ControlTemplate>
<ControlTemplate>
<Grid Padding="0" RowSpacing="0" ColumnSpacing="0">
<StackLayout>
<StackLayout.Padding>
<OnPlatform x:TypeArguments="Thickness" iOS="0,30,0,0"/>
</StackLayout.Padding>
<ScrollView VerticalOptions="FillAndExpand" >
<StackLayout RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height}" Orientation="Vertical">
<ContentPresenter />
</StackLayout>
</ScrollView>
</StackLayout>
<AbsoluteLayout IsVisible="False" Grid.Row="0" Grid.Column="0" x:Name="loading" BackgroundColor="#85000000" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0,0,1,1">
<ActivityIndicator Color="Lime" Scale="2" IsRunning="true" IsEnabled="true" IsVisible="true" AbsoluteLayout.LayoutFlags="PositionProportional" AbsoluteLayout.LayoutBounds="0.5,0.4" />
<Button IsVisible="True" Text="{TemplateBinding MyTextLabel}" AbsoluteLayout.LayoutFlags="PositionProportional" AbsoluteLayout.LayoutBounds="0.5,0.5" Command="{TemplateBinding MyCommand}" />
</AbsoluteLayout>
</Grid>
</ControlTemplate>
</ContentPage.ControlTemplate>
</ContentPage>
And here's the C# for that BasePage:
using System;
using System.Windows.Input;
using ShoppingListNEW.Views;
using Xamarin.Forms;
namespace ShoppingListNEW.Pages
{
public partial class BasePage : ContentPage
{
public string MyTextLabel { get; set; } = "This Works";
public ICommand MyCommand { get; set; }
public BasePage()
{
InitializeComponent();
MyCommand = new Command(() =>
{
Console.Write("But this doesn't work :(");
});
}
public void ShowHideLoading(bool showhide, bool allowCancel = false)
{
var loadingLayout = (AbsoluteLayout)GetTemplateChild("loading");
var cancelButton = loadingLayout.FindByName<Button>("btnCancel");
cancelButton.IsVisible = allowCancel;
loadingLayout.IsVisible = showhide;
}
}
}
Any ideas?
On your ControlTemplate binding, please use the following code:
<Button
AbsoluteLayout.LayoutBounds="0.5,0.5"
AbsoluteLayout.LayoutFlags="PositionProportional"
Command="{TemplateBinding BindingContext.MyCommand}"
IsVisible="True"
Text="{TemplateBinding BindingContext.MyTextLabel}" />
I use your code to create simple that you can take a look, please don't set AbsoluteLayout IsVisible="False".
<ContentPage.ControlTemplate>
<ControlTemplate>
<Grid Padding="0">
<StackLayout>
<ScrollView VerticalOptions="FillAndExpand">
<StackLayout
Orientation="Vertical"
RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height}"
RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Width}">
<ContentPresenter />
</StackLayout>
</ScrollView>
</StackLayout>
<AbsoluteLayout
x:Name="loading"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All"
BackgroundColor="#85000000">
<ActivityIndicator
AbsoluteLayout.LayoutBounds="0.5,0.4"
AbsoluteLayout.LayoutFlags="PositionProportional"
IsEnabled="true"
IsRunning="true"
IsVisible="true"
Scale="2"
Color="Lime" />
<Button
AbsoluteLayout.LayoutBounds="0.5,0.5"
AbsoluteLayout.LayoutFlags="PositionProportional"
Command="{TemplateBinding BindingContext.MyCommand}"
IsVisible="True"
Text="{TemplateBinding BindingContext.MyTextLabel}" />
</AbsoluteLayout>
</Grid>
</ControlTemplate>
</ContentPage.ControlTemplate>
public partial class Page19 : ContentPage
{
public Page19()
{
InitializeComponent();
this.BindingContext = new Viewmodel1();
}
}
public class Viewmodel1:ViewModelBase
{
private string _MyTextLabel;
public string MyTextLabel
{
get { return _MyTextLabel; }
set
{
_MyTextLabel = value;
RaisePropertyChanged("MyTextLabel");
}
}
public RelayCommand MyCommand { get; set; }
public Viewmodel1()
{
MyCommand = new RelayCommand(method);
MyTextLabel = "this is test";
}
private void method()
{
Console.WriteLine("this is test!");
}
}
Problem solved. Thanks to Mikolaj Kieres for the answer, it was a simple case of initialising the Command earlier so I moved it up above the InitializeComponent and that kicked it into life. Thanks!
I have created a custom stepper Behavior, and added that behavior to a stepper in my xaml, but for some reason after adding the behavior the application doesn't compile, and i get this error:
Position 82:87. No property, bindable property, or event found for 'ValueChangedCommand', or mismatching type between value and property. (ComanderoMovil)
here is my code of the behavior:
using System;
using System.Windows.Input;
using Xamarin.Forms;
namespace ComanderoMovil.Behaviors
{
public class StepperQuantityChangedBehavior : Behavior<Stepper>
{
public static readonly BindableProperty StepperValueChangedProperty =
BindableProperty.Create("ValueChangedCommand", typeof(ICommand), typeof(StepperQuantityChangedBehavior), null);
public ICommand ValueChangedCommand
{
get
{
return (ICommand)GetValue(StepperValueChangedProperty);
}
set
{
SetValue(StepperValueChangedProperty, value);
}
}
protected override void OnAttachedTo(Stepper bindable)
{
base.OnAttachedTo(bindable);
bindable.ValueChanged += Bindable_ValueChanged;
}
protected override void OnDetachingFrom(Stepper bindable)
{
base.OnDetachingFrom(bindable);
bindable.ValueChanged -= Bindable_ValueChanged;
}
private void Bindable_ValueChanged(object sender, ValueChangedEventArgs e)
{
if (ValueChangedCommand == null)
{
return;
}
var stepper = sender as Stepper;
var prueba = e.NewValue;
if (ValueChangedCommand.CanExecute(prueba))
{
ValueChangedCommand.Execute(prueba);
}
}
}
}
and here is my code of the xaml where I add the behavior:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ComanderoMovil.Views.DishView"
xmlns:converterPack="clr-namespace:Xamarin.Forms.ConvertersPack;assembly=Xamarin.Forms.ConvertersPack"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
ios:Page.UseSafeArea="true"
xmlns:local="clr-namespace:ComanderoMovil.Behaviors"
x:Name="DishSelectedPage">
<ContentPage.ToolbarItems>
<ToolbarItem Icon="shopping_cart" Text="Search" Command="{Binding ShowCartCommand}" />
</ContentPage.ToolbarItems>
<ContentPage.Resources>
<ResourceDictionary>
<converterPack:CurrencyConverter x:Key="CurrencyConverter"></converterPack:CurrencyConverter>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<ScrollView>
<StackLayout>
<Label Text="{Binding Dish.Name}"
FontSize="Title"
HorizontalOptions="Center"
FontAttributes="Bold"></Label>
<Label Text="Precio"
FontSize="Subtitle"
HorizontalOptions="Center"
FontAttributes="Bold"></Label>
<Label Text="{Binding Dish.Price1, Converter={StaticResource CurrencyConverter}}"
FontSize="Subtitle"
HorizontalOptions="Center"></Label>
<Label Text="Modificadores"
FontAttributes="Bold"
FontSize="Large"
HorizontalOptions="Center"></Label>
<ListView ItemsSource="{Binding DishesMods}"
x:Name="ModsListView"
HasUnevenRows="True"
SeparatorVisibility="Default"
SeparatorColor="Black"
IsGroupingEnabled="True"
HeightRequest="{Binding ListHeight}">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell Height="30">
<StackLayout VerticalOptions="FillAndExpand"
Padding="10"
BackgroundColor="DimGray">
<Label Text="{Binding Key}"
TextColor="White"
VerticalOptions="Center"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="20">
<StackLayout Orientation="Horizontal">
<CheckBox Color="#102536">
<CheckBox.Behaviors>
<local:CheckBoxModChangedState ItemCheckedCommand="{Binding BindingContext.SelectedModCommand, Source={Reference DishSelectedPage}}"></local:CheckBoxModChangedState>
</CheckBox.Behaviors>
</CheckBox>
<Label Text="{Binding Name}"
VerticalOptions="Center"></Label>
<Label Text="Precio:"
VerticalOptions="Center"></Label>
<Label Text="{Binding Price}"
VerticalOptions="Center"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Cantidad: "></Label>
<Label Text="1"></Label>
</StackLayout>
<StackLayout>
<Stepper HeightRequest="40"
WidthRequest="40">
<Stepper.Behaviors>
<local:StepperQuantityChangedBehavior ValueChangedCommand="{Binding BindingContext.ModQuantityCommand, Source={Reference DishSelectedPage}}"></local:StepperQuantityChangedBehavior>
</Stepper.Behaviors>
</Stepper>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Footer>
<ContentView>
<Frame HasShadow="False"
Padding="50">
<Button Padding="20"
Text="Agregar Orden"
TextColor="White"
BackgroundColor="#102536"
Command="{Binding BindingContext.AddOrderCommand, Source={Reference DishSelectedPage}}"></Button>
</Frame>
</ContentView>
</ListView.Footer>
</ListView>
</StackLayout>
</ScrollView>
</ContentPage.Content>
</ContentPage>
the funny thing is that, I have added custom behavior for other controls, like the checkbox, and it works without a problem, but only with this new behavior I am having trouble.
Anyone know's what is happening?
public static readonly BindableProperty StepperValueChangedProperty
public ICommand ValueChangedCommand
the issue is on the lines,as the error message said
event found for 'ValueChangedCommand', or mismatching type between
value and property. (ComanderoMovil)
you should change StepperValueChangedProperty to ValueChangedCommandProperty to keep the name consistent with ValueChangedCommand
change
public static readonly BindableProperty StepperValueChangedProperty =
BindableProperty.Create("ValueChangedCommand", typeof(ICommand), typeof(StepperQuantityChangedBehavior), null);
public ICommand ValueChangedCommand
{
get
{
return (ICommand)GetValue(StepperValueChangedProperty);
}
set
{
SetValue(StepperValueChangedProperty, value);
}
}
to
public static readonly BindableProperty ValueChangedCommandProperty =
BindableProperty.Create("ValueChangedCommand", typeof(ICommand), typeof(StepperQuantityChangedBehavior), null);
public ICommand ValueChangedCommand
{
get
{
return (ICommand)GetValue(ValueChangedCommandProperty);
}
set
{
SetValue(ValueChangedCommandProperty, value);
}
}
I'm trying to create a XAML template in Xamarin Forms that I can reference from multiple screens. The template has a header, a summary, and can receive touch input. In the attached wireframe, there would be a model backing the three blocks of data. I've read through the docs at https://developer.xamarin.com/guides/xamarin-forms/templates/control-templates/template-binding/, but at this point I feel like I'm missing an important concept. I thought I would just need to create a ContentView template and somehow make the Labels inside bindable to an object at runtime via .BindingContext. What am I missing?
Content Page
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Namespace.SettingsPage"
Title ="Settings">
<ContentPage.Resources>
<ResourceDictionary>
<ControlTemplate x:Key="SettingTemplate">
<StackLayout>
<Label
x:Name="Header"
Text="{TemplateBinding Parent.Header}" />
<Label
x:Name="Summary"
Text="{TemplateBinding Parent.Summary}"/>
</StackLayout>
</ControlTemplate>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<StackLayout>
<ContentView x:Name="alerts" ControlTemplate="{StaticResource SettingTemplate}"></ContentView>
<ContentView x:Name="recipients" ControlTemplate="{StaticResource SettingTemplate}"></ContentView>
<ContentView x:Name="log" ControlTemplate="{StaticResource SettingTemplate}"></ContentView>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Code Behind
using Xamarin.Forms;
namespace Namespace.Settings
{
public partial class SettingsPage : ContentPage
{
protected SettingsViewModel viewModel;
public SettingsPage(AlertInfo info)
{
InitializeComponent();
BindingContext = viewModel = new SettingsViewModel(info, this);
// Bind individual models here. AlertSummary VM has logic to pull out
// header and summary fiels.
this.alerts.BindingContext = new AlertSummaryViewModel(info, Summary.ALERTS, this);
this.recipients.BindingContext = new AlertSummaryViewModel(info, Summary.RECIPIENTS, this);
}
}
}
Here is the starting point.
public partial class ControlTemplateTest : ContentPage
{
public static readonly BindableProperty HeaderTextProperty =
BindableProperty.Create("HeaderText", typeof(string), typeof(ControlTemplateTest), "Alerts");
public static readonly BindableProperty SummaryTextProperty =
BindableProperty.Create("SummaryText", typeof(string), typeof(ControlTemplateTest), "Three alerts set");
public string HeaderText
{
get { return (string)GetValue(HeaderTextProperty); }
}
public string SummaryText
{
get { return (string)GetValue(SummaryTextProperty); }
}
public ControlTemplateTest()
{
InitializeComponent();
}
}
}
Xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ButtonRendererDemo.ControlTemplateTest">
<ContentPage.Resources>
<ResourceDictionary>
<ControlTemplate x:Key="SettingTemplate">
<StackLayout BackgroundColor="#DCF394" >
<Label FontSize="48" TextColor="Black" Text="{TemplateBinding Parent.Parent.HeaderText}" />
<Label Text="{TemplateBinding Parent.Parent.SummaryText}"/>
</StackLayout>
</ControlTemplate>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout BackgroundColor="White" VerticalOptions="FillAndExpand" Spacing="0,40" Padding="0,40">
<ContentView x:Name="alerts" VerticalOptions="FillAndExpand" ControlTemplate="{StaticResource SettingTemplate}"></ContentView>
<ContentView x:Name="recipients" VerticalOptions="FillAndExpand" ControlTemplate="{StaticResource SettingTemplate}"></ContentView>
<ContentView x:Name="log" VerticalOptions="FillAndExpand" ControlTemplate="{StaticResource SettingTemplate}"></ContentView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
i tried to move a the master in MasterDetailPage into separate file. As soon as i do this, the MD Page is not longer loaded.
MasterDetailPage:
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
xmlns:local="clr-namespace:PrismMasterDetailSample.Views;assembly=PrismMasterDetailSample"
x:Class="PrismMasterDetailSample.Views.PMasterDetailPage">
<MasterDetailPage.Master Title="Menu">
<!--<local:MenuPage /> <========== does not work -->
<ContentPage Title="Default">
<StackLayout>
<Button Text="About" Command="{Binding NavigateCommand}" CommandParameter="MyNavigationPage/ViewA?id=A" />
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<local:PTabbedPage />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
Codebehind:
using Xamarin.Forms;
namespace PrismMasterDetailSample.Views
{
public partial class PMasterDetailPage : MasterDetailPage
{
public PMasterDetailPage()
{
InitializeComponent();
}
}
}
Here is the MenuPage XAML
"1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="PrismMasterDetailSample.Views.MenuPage"
Title="Menu">
<StackLayout VerticalOptions="End" HorizontalOptions="Center" Margin="20">
<Button Text="DetailPage1" Command="{Binding NavigateCommand}" HorizontalOptions ="LayoutOptions.Center"/>
</StackLayout>
</ContentPage>
The Code behind:
using Xamarin.Forms;
namespace PrismMasterDetailSample.Views
{
public partial class MenuPage : ContentPage
{
public MenuPage()
{
InitializeComponent();
}
}
}
And the ViewModel:
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PrismMasterDetailSample.ViewModels
{
public class MenuPageViewModel : BindableBase
{
public DelegateCommand NavigateCommand { get; private set; }
private INavigationService _navigationService;
public MenuPageViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
NavigateCommand = new DelegateCommand(Navigate);
}
private void Navigate()
{
_navigationService.NavigateAsync("DetailPage1");
}
}
}
I'm quite sure that is a trivial issue, but i didn't get the point.
Many thanks in advance.
And here is the answer ;-D
Add "Title" to the MenuPage
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="PrismMasterDetailSample.Views.MenuPage"
Title="Menu">
<StackLayout VerticalOptions="End" HorizontalOptions="Center" Margin="20">
<Button Text="DetailPage1" Command="{Binding NavigateCommand}" HorizontalOptions ="LayoutOptions.Center"/>
</StackLayout>
</ContentPage>
I am currently doing a test with ICommand and I wonder why ICommand does not fire with button in ListBox.ItemTemplate. But when used outside the template, it works.
here's the window xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1" x:Class="WpfApplication1.Window2"
Title="Window2" Height="300" Width="300">
<Window.DataContext>
<local:W2VM/>
</Window.DataContext>
<Grid>
<ListBox x:Name="listHistory" BorderThickness="0" Margin="0" Padding="0" HorizontalContentAlignment="Stretch" ItemsSource="{Binding History}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding ''}" />
<Button
Grid.Column="1"
HorizontalAlignment="Right"
x:Uid="btnDeleteHistoryItem"
x:Name="btnDeleteHistoryItem"
Content="r"
FontFamily="Marlett"
Visibility="Hidden" Command="{Binding MeClick}"
/>
</Grid>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Visibility" TargetName="btnDeleteHistoryItem" Value="Visible" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content=" exit " VerticalAlignment="Bottom" Command="{Binding ExitCommand}" />
</Grid>
</Window>
here's the complete ViewModel code
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Diagnostics;
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.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window2.xaml
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
}
public class W2VM : ViewModelBase
{
public List<string> _History = new List<string>();
public List<string> History
{
get { return this._History; }
}
public ICommand MeClick
{
get;
internal set;
}
public ICommand ExitCommand
{
get;
internal set;
}
public W2VM()
{
this.History.AddRange(new string[] {
"jayson", "hello", "world"
});
this.MeClick = new RelayCommand(Test);
this.ExitCommand = new RelayCommand(Exit);
}
void Exit()
{
Application.Current.Shutdown();
}
void Test()
{
Debug.WriteLine("hello world");
MessageBox.Show("do something incredible");
}
}
}
Test() does not fire
ok I got it working..
<Window.Resources>
<me:W2VM x:Key="local" />
</Window.Resources>
instead of
<Window.DataContext>
<local:W2VM/>
</Window.DataContext>
and my window grid
<Grid DataContext="{StaticResource local}">
<ListBox x:Name="listHistory" BorderThickness="0" Margin="0" Padding="0" HorizontalContentAlignment="Stretch" ItemsSource="{Binding History}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding ''}" />
<Button
Grid.Column="1"
HorizontalAlignment="Right"
x:Uid="btnDeleteHistoryItem"
x:Name="btnDeleteHistoryItem"
Content="r"
FontFamily="Marlett"
Visibility="Hidden" Command="{Binding MeClick, Source={StaticResource local}}" />
</Grid>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Visibility" TargetName="btnDeleteHistoryItem" Value="Visible" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content=" exit " VerticalAlignment="Bottom" Command="{Binding ExitCommand}" />
</Grid>
now this will raise another question.
How would I know which ListViewItem was clicked when the btnDeleteHistoryItem command was just routed?
Are you using the relaycommand of mvvm-light?
I use command parameters to pass information to the command method:
<Button
Grid.Column="1"
HorizontalAlignment="Right"
x:Uid="btnDeleteHistoryItem"
x:Name="btnDeleteHistoryItem"
Content="r"
FontFamily="Marlett"
Visibility="Hidden" Command="{Binding MeClick, Source=StaticResource local}}"
CommandParameter="YourUniqueIdentifyingVariable" />
and then in your view model:
public RelayCommand<string> MeClick{
get { return _MyClick; }
private set { _MyClick = value; }
}
public New()
{
MeClick = new RelayCommand<string>(MySub, MySubIsEnabled);
}
private void MySub(string sParam)
{
//do stuff here, YourUniqueIdentifyingVariable is sParam
}
private bool MySubIsEnabled(string sParam)
{
return true;
}
you can bind "YourUniqueIdentifyingVariable" to the ID of the record or some other value that you can process in your command method
Code is untested but should work