codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

MVVM - Access command from different class in XAML


By : , Category : c#

I've fixed the problem. All I had to was this:

<Button Content="New Game"
            Command="{Binding GameViewCommand}"
            DataContext="{Binding Main, Source={StaticResource Locator}}"
            Grid.Column="1"
            Grid.Row="1"
            HorizontalAlignment="Center"
            VerticalAlignment="Center" />

<Button Content="Say Hello"  
            Command="{Binding SayHelloCommand}"               
            Grid.Column="2"
            Grid.Row="2"
            HorizontalAlignment="Center"
            VerticalAlignment="Center" />

Turns out it was a very easy fix.

ReLated :

Try changing the command binding as I show below:

<Button Command="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext.ButtonClickCommand_DisplayFailureDetails}" ...> 

This way you can get the DataContext property of your window which was set with an instance of your ViewModel.

Try this code :

<phone:PhoneApplicationPage
x:Class="SparrowCharts.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clrnamespace: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:sparrow="clr-namespace:Sparrow.Chart;assembly=Sparrow.Chart.WP8.45"
xmlns:local="clr-namespace:MyApplication"

mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">

<phone:PhoneApplicationPage.DataContext>
    <local:ViewModel />
</phone:PhoneApplicationPage.DataContext>

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>



    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
        <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <sparrow:SparrowChart>
            <sparrow:SparrowChart.XAxis>
                <sparrow:LinearXAxis/>
            </sparrow:SparrowChart.XAxis>
            <sparrow:SparrowChart.YAxis>
                <sparrow:LinearYAxis/>
            </sparrow:SparrowChart.YAxis>
            <sparrow:LineSeries PointsSource="{Binding DataContext.Collection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type phone:PhoneApplicationPage}}}" XPath="X" YPath="Y"/>
        </sparrow:SparrowChart>
    </Grid>
</Grid>

2 ways:

<ListView ItemsSource="{Binding Items}">
        <ListView.InputBindings>
            <MouseBinding Gesture="LeftDoubleClick" Command="{Binding MyListViewCommand}"/>
        </ListView.InputBindings>
        <ListView.ItemTemplate>
            <DataTemplate DataType="{x:Type local:Item}">
                <TextBlock Text="{Binding Text}">
                    <TextBlock.InputBindings>
                        <MouseBinding Gesture="LeftDoubleClick" Command="{Binding MyItemCommand}"/>
                    </TextBlock.InputBindings>
                </TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>            
    </ListView>

Or bind the even to a command:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

<ListView>

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SomeEvent">
            <i:InvokeCommandAction Command="{Binding Path=SomeCommand, Mode=OneWay}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

</ListView>

If you want to have button which adds new items to your collection, I think something like that can be the solution.

In XAML:

<GridView ItemsSource="{Binding DeckCollection}" IsItemClickEnabled="True" Grid.Row="0">
        <GridView.ItemTemplate>
            <DataTemplate>
                <Button>
                    <Button.Content>
                        <Grid>
                            <Image Source="{Binding ImagePath}"
                                Stretch="None"/>
                        </Grid>
                    </Button.Content>
                </Button>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
<!-- public property located in Deck class -->
<Button Command="{Binding AddItemCommand}" Content="Add Item"/>

In C#:

    class Deck, INotifyPropertyChanged /*custom implementation depends on .NET version, in my case its .NET3.5*/
    {
    private ObservableCollection<Card> _deckCollection = new ObservableCollection<Card>();
    public ObservableCollection<Card> DeckCollection
    {
        get { return _deckCollection; }
        set { _deckCollection = value; 
              OnPropertyChanged(() => DeckCollection); }
    }

    // your Add command
    public ICommand AddItemCommand { get { return new MyCommand(AddToList); } }
    private void AddToList(object parameter)
    {
       DeckCollection.Add(new Card());
    }

    public Deck() { }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged<T>(Expression<Func<T>> expression) 
    {
       if (PropertyChanged == null) return;

       var body = (MemberExpression)expression.Body;
       if (body != null) PropertyChanged.Invoke(this, new PropertyChangedEventArgs(body.Member.Name));
    }
}

The main thing in this situation is that you cannot have the add button inside the collection.

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .