I created datatemplate for my listview with 2 button that must user click on if i use my datatemplate in Window everything's is OK and work fine but if I use my datatemplate in Usercontrol click event for my buttons not work so what is the problem? this is my codes:
<ListView Name="lvDataBinding" HorizontalContentAlignment="Stretch" BorderThickness="0" Margin="10" Grid.Row="3" Background="{x:Null}" SelectionChanged="lvDataBinding_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<Border Background="#f0f4f7">
<StackPanel Background="#f5f6fa" Margin="1,1,1,1" VerticalAlignment="Top">
<Border Background="#edf0f5" BorderThickness="5">
<Grid Background="#ffffff" Height="30">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Background="#ffffff" Margin="5" Orientation="Horizontal">
<Button Height="20" Width="20" BorderBrush="Transparent" BorderThickness="0" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.cmddelete}">
<Button.Background>
<ImageBrush ImageSource="E:\Aks\ICON\colorful-stickers-icons-set\png\32x32\accept.png"/>
</Button.Background>
</Button>
<Button Height="20" Width="20" BorderBrush="Transparent" BorderThickness="0" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.cmdShow}">
<Button.Background>
<ImageBrush ImageSource="E:\Aks\ICON\colorful-stickers-icons-set\png\32x32\accept.png"/>
</Button.Background>
</Button>
</StackPanel>
<TextBlock Name="txtPhon" Foreground="#7c7f84" HorizontalAlignment="Right" Grid.Column="1" Text="{Binding Path=HomePhoneNumber}"
Margin="0,5,5,5"/>
</Grid>
</Border>
</StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And this is my code behind that I created iCommand and bind it to buttons :
public ICommand cmdShow { get; set; }
public ICommand cmddelete { get; set; }
private FrameworkElement Window { get; set; }
int index = 0;
public UserControl1()
{
InitializeComponent();
InitalizeData();
this.DataContext = this;
Window = this;
cmdShow = new RoutedCommand();
cmddelete = new RoutedCommand();
CommandManager.RegisterClassCommandBinding(Window.GetType(), new CommandBinding(cmdShow, cmdShow_Click));
CommandManager.RegisterClassCommandBinding(Window.GetType(), new CommandBinding(cmddelete, cmddelete_Click));
}
protected void cmdShow_Click(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show(lvDataBinding.SelectedIndex + "");
}
protected void cmddelete_Click(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("delete");
}
private void InitalizeData()
{
ObservableCollection<Patient> data = new ObservableCollection<Patient>();
for (int i = 0; i < 3; i++)
{
data.Add(new Patient
{
HomePhoneNumber = "0512-62810609"
});
}
this.lvDataBinding.ItemsSource = data;
}
public class Patient
{
public string HomePhoneNumber { get; set; }
}
Depending where you set the DataContext the Problem is probably caused by
AncestorType={x:Type **Window**}}
Related
In my XAML I have a ContextMenu where the MenuItems are associated with commands:
<ToolBarTray Background="Black" Width="180" Height="25" VerticalAlignment="Top" HorizontalAlignment="Right" Grid.Column="1">
<ToolBar Background="Black">
<ToolBar.Clip>
<RectangleGeometry Rect="10,0,54,25" />
</ToolBar.Clip>
<ToggleButton Checked="DropdownButton_Checked" MouseRightButtonUp="DropdownButton_MouseRightButtonUp" Background="Black" Margin="-4,-6,4,0">
<ToggleButton.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Layout" Foreground="White"/>
<Path Margin="4" Width="5" Fill="White" Stretch="Uniform" HorizontalAlignment="Right" VerticalAlignment="Center" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z" />
</StackPanel>
</ToggleButton.Content>
<ToggleButton.ContextMenu>
<ContextMenu Closed="ContextMenu_Closed">
<MenuItem Header="1x1" Command="{Binding MenuItem1}"/>
<MenuItem Header="2x2" Command="{Binding MenuItem2}"/>
<MenuItem Header="3x3" Command="{Binding MenuItem3}"/>
<MenuItem Header="1+5" Command="{Binding MenuItem4}"/>
</ContextMenu>
</ToggleButton.ContextMenu>
</ToggleButton>
</ToolBar>
</ToolBarTray>
I use these commands to toggle the layout of another part of the markup, e.g. Layout1 looks like this:
<Grid Grid.Row="1" Opacity="{Binding Layout1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<Border BorderBrush="White" BorderThickness="1" />
</Grid>
For this I have the following code in the view model:
public int Layout1 { get; set; }
public IReactiveCommand MenuItem1 { get; }
public MainWindowViewModel()
{
MenuItem1 = ReactiveCommand.Create(OnMenuItem1);
}
internal void OnMenuItem1()
{
Layout1 = 100;
Layout2 = 0;
Layout3 = 0;
Layout4 = 0;
}
And the same for Layout2, Layout3, Layout4.
Now when running this with the debugger I see that when I select "1x1" from the menu, OnMenuItem1 is executed. But the layout is not displayed on the screen.
Try this:
private int _layout1;
public int Layout1 { get => _layout1; set { RaiseAndSetIfChanged(ref _layout1, value); } }
How can I make the dropdown sub-items clickable? i.e if I click on 'Employees' it will open the Employees page. I copied this from a tutorial however they didn't explain how to make click events with this creation.
Subitem Class
public class SubItem
{
public SubItem(string name, UserControl screen = null)
{
Name = name;
Screen = screen;
}
public string Name { get; private set; }
public UserControl Screen { get; private set; }
}
}
ItemMenu Class
public class ItemMenu
{
public ItemMenu(string header, List subItems, PackIconKind icon)
{
Header = header;
SubItems = subItems;
Icon = icon;
}
public ItemMenu(string header, UserControl screen, PackIconKind icon)
{
Header = header;
Screen = screen;
Icon = icon;
}
public string Header { get; private set; }
public PackIconKind Icon { get; private set; }
public List<SubItem> SubItems { get; private set; }
public UserControl Screen { get; private set; }
}
}
**UserControlMenuItem.xaml**
<Grid>
<materialDesign:PackIcon Kind="{Binding Path=Icon}" Width="15" Height="15" Margin="10 16" Foreground="White"/>
<ListBoxItem x:Name="ListViewItemMenu" Content="{Binding Path=Header}" Padding="37 14" FontSize="15" Foreground="White"/>
<Expander x:Name="ExpanderMenu" Header="{Binding Path=Header}" IsExpanded="False" Width="210" HorizontalAlignment="Right" Background="{x:Null}" Foreground="White">
<ListView x:Name="ListViewMenu" ItemsSource="{Binding Path=SubItems}" Foreground="White" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" Padding="20 5"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Expander>
</Grid>
**MainWindow.xaml**
<materialDesign:ColorZone Mode="PrimaryMid" Grid.ColumnSpan="2" HorizontalAlignment="Stretch">
<Grid>
<materialDesign:PopupBox PlacementMode="BottomAndAlignRightEdges" HorizontalAlignment="Right" Margin="10"/>
</Grid>
</materialDesign:ColorZone>
<Grid HorizontalAlignment="Stretch" Grid.Row="1" Background="{StaticResource PrimaryHueMidBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="70"/>
<RowDefinition Height="326*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="GhostWhite">
<Image Source="Images/logo.png"/>
</Grid>
<ScrollViewer HorizontalAlignment="Stretch" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" Grid.Row="1">
<StackPanel x:Name="Menu" Margin="10" />
</ScrollViewer>
</Grid>
<Frame Source="/CSA;component/Pages/Landing.xaml" Grid.Row="1" x:Name="ContentFrame" Grid.Column="1" Grid.ColumnSpan="2" Grid.RowSpan="2" Background="#2C2F33" Opacity="0.85" NavigationUIVisibility="Hidden" />
</Grid>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var menuRegister = new List<SubItem>();
menuRegister.Add(new SubItem("Customer"));
menuRegister.Add(new SubItem("Providers"));
menuRegister.Add(new SubItem("Employees"));
menuRegister.Add(new SubItem("Products"));
var item6 = new ItemMenu("Register", menuRegister, PackIconKind.Register);
var menuSchedule = new List<SubItem>();
menuSchedule.Add(new SubItem("Services"));
menuSchedule.Add(new SubItem("Meetings"));
var item1 = new ItemMenu("Appointments", menuSchedule, PackIconKind.Schedule);
var menuReports = new List<SubItem>();
menuReports.Add(new SubItem("Customers"));
menuReports.Add(new SubItem("Providers"));
menuReports.Add(new SubItem("Products"));
menuReports.Add(new SubItem("Stock"));
menuReports.Add(new SubItem("Sales"));
var item2 = new ItemMenu("Reports", menuReports, PackIconKind.FileReport);
var menuExpenses = new List<SubItem>();
menuExpenses.Add(new SubItem("Fixed"));
menuExpenses.Add(new SubItem("Variable"));
var item3 = new ItemMenu("Expenses", menuExpenses, PackIconKind.ShoppingBasket);
var menuFinancial = new List<SubItem>();
menuFinancial.Add(new SubItem("Cash flow"));
var item4 = new ItemMenu("Financial", menuFinancial, PackIconKind.ScaleBalance);
var item0 = new ItemMenu("Dashboard", new UserControl(), PackIconKind.ViewDashboard);
Menu.Children.Add(new UserControlMenuItem(item0));
Menu.Children.Add(new UserControlMenuItem(item6));
Menu.Children.Add(new UserControlMenuItem(item1));
Menu.Children.Add(new UserControlMenuItem(item2));
Menu.Children.Add(new UserControlMenuItem(item3));
Menu.Children.Add(new UserControlMenuItem(item4));
}
```
All you need is to make the thing in ListView's ItemTemplate clickable. The easiest way to do that is to use a Button instead of a TextBlock. You can disable the Button's background and border to make it appear like text, but remain clickable.
Also, you don't need to use a ListView within an Expander. ListView offers scrolling capabilities and you don't need that. An ItemsControl will be enough.
Here's an example:
<Expander
Header="Expand me">
<ItemsControl ItemsSource="{Binding SubItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button
Background="Transparent"
BorderThickness="0"
Command="{Binding DataContext.DoSomethingCommand, ElementName=ThisWindow}"
Content="{Binding Name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Expander>
I'm binding to the same ICommand for all the sub items in this example. Realisticly, you would either pass a CommandParameter as well for the different sub items or bind to a different command per item.
After hours of searching the web I found a solution.
Method
public static void Navigate(object target)
{
((MainWindow)Application.Current.Windows[0]).ContentFrame.Content = target;
}
Implementation
switch (Globals.PageName)
{
case "Landing":
Pages.Landing itemZ = new Pages.Landing();
Navigate(itemZ);
break;
case "ClientAcquisition":
Pages.ClientAcquisition item = new Pages.ClientAcquisition();
Navigate(item);
break;
I have two radio buttons in a group as part of my XAML project:
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Center" Orientation="Horizontal" Grid.Column="0" Margin="20,0,0,0">
<RadioButton x:Name="XMLViewButton" GroupName="DisplayType" IsChecked="{Binding XmlViewIsChecked, FallbackValue=True, Mode=TwoWay}" Content="XML View" Margin="0,0,5,0"/>
<RadioButton x:Name="TextViewButton" GroupName="DisplayType" IsChecked="{Binding TextViewIsChecked, FallbackValue=False, Mode=TwoWay}" Content="Text View" Margin="5,0,0,0"/>
</StackPanel>
And I then have a command later on which refers to these IsChecked bindings:
public void CopyToClipboard(object o)
{
if (TextViewIsSelected == true)
{
Clipboard.SetText(myFile.TextContent);
}
else if (XmlViewIsSelected == true)
{
Clipboard.SetText(myFile.XMLContent);
}
}
However, the XmlViewIsSelected is permanently True and TextViewIsSelected is always false, no matter which radio button is selected. What am I missing?
I think you are mispelling XmlViewIsChecked with XmlViewIsSelected
The following for me is working
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Center" Orientation="Horizontal" Grid.Column="0" Margin="20,0,0,0">
<RadioButton x:Name="XMLViewButton" GroupName="DisplayType" IsChecked="{Binding XmlViewIsChecked, FallbackValue=True, Mode=TwoWay}" Content="XML View" Margin="0,0,5,0"/>
<RadioButton x:Name="TextViewButton" GroupName="DisplayType" IsChecked="{Binding TextViewIsChecked, FallbackValue=False, Mode=TwoWay}" Content="Text View" Margin="5,0,0,0"/>
<Button Content="Check" Click="Button_Click" />
</StackPanel>
public partial class MainWindow : Window
{
public bool XmlViewIsChecked { get; set; }
public bool TextViewIsChecked { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (TextViewIsChecked)
{
Clipboard.SetText("text");
}
else if (XmlViewIsChecked)
{
Clipboard.SetText("xml");
}
}
}
I want to have a Delete button for every element in my ListView, I searched stackoverflow but none answer my question.
I tried giving my ListView a x:Name="QuizListView" and using ElementName inside the ListView to bind the command to the button.
QuizOverview.xaml
<Page
x:Name="QuizOverviewPage"
x:Class="UwpApp.View.QuizOverview"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UwpApp.View"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
DataContext="{Binding QuizOverviewInstance, Source={StaticResource Locator}}">
<StackPanel Margin="20">
<TextBlock Text="Quizzen" FontSize="48"/>
<ListView x:Name="QuizListView" ItemsSource="{Binding Quizzes}" Margin="0,20" SelectionMode="Single" SelectionChanged="QuizListView_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<ListViewItem>
<StackPanel Orientation="Horizontal">
<Border CornerRadius="10" BorderBrush="Black" BorderThickness="2" Height="100" Width="400" VerticalAlignment="Center">
<TextBlock Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="36"></TextBlock>
</Border>
<Button VerticalAlignment="Center" Padding="20" Margin="20">Edit</Button>
<Button VerticalAlignment="Center" Padding="20" Margin="0" Command="{Binding Path=DataContext.DeleteQuizCommand, ElementName=QuizListView}" CommandParameter="{Binding}">Delete</Button>
</StackPanel>
</ListViewItem>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Padding="20" Command="{Binding CreateQuizCommand}">Add New Quiz</Button>
</StackPanel>
</Page>
QuizOverviewViewModel.cs
public class QuizOverviewViewModel : ViewModelBase
{
public string Title { get; set; }
public ObservableCollection<Quiz> Quizzes { get; set; }
public RelayCommand<Quiz> DeleteQuizCommand { get; set; }
public RelayCommand CreateQuizCommand { get; set; }
public QuizOverviewViewModel()
{
Title = "Quizzen";
Quizzes = new ObservableCollection<Quiz> { new Quiz(1, "Test Quiz 1"), new Quiz(2, "Test Quiz 2"), new Quiz(3, "Test Quiz 3") };
DeleteQuizCommand = new RelayCommand<Quiz>(DeleteQuiz);
CreateQuizCommand = new RelayCommand(CreateQuizTest);
}
public void DeleteQuiz(Quiz quiz)
{
Quizzes.Remove(Quizzes.Single(i => i.Id == quiz.Id));
}
public void CreateQuizTest()
{
Quizzes.Add(new Quiz(4, "Test Quiz Creation!"));
}
}
Quiz.cs
namespace UwpApp.Model
{
public class Quiz
{
public long Id { get; set; }
public string Name { get; set; }
public Quiz(long id, string name)
{
Id = id;
Name = name;
}
}
}
The Button does nothing with my current code, while the command does work outside of the ListView.
Databinding to command inside of listview not working in UWP/MVVM-Light
The problem is that you insert ListViewItem in to DataTemplate. It will cause that ListViewItem contains sub-ListViewItem in the Visual Tree and you could not access correct DataContext with element name in your button. Please remove ListViewItem from your code.
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border CornerRadius="10" BorderBrush="Black" BorderThickness="2" Height="100" Width="400" VerticalAlignment="Center">
<TextBlock Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="36"></TextBlock>
</Border>
<Button VerticalAlignment="Center" Padding="20" Margin="20">Edit</Button>
<Button VerticalAlignment="Center" Padding="20" Margin="0" Command="{Binding Path=DataContext.DeleteQuizCommand, ElementName=QuizListView}" CommandParameter="{Binding}">Delete</Button>
</StackPanel>
</DataTemplate>
I have done something very bad, because I cannot get it to work at all the way I think I should do it.
I am writing a small test harness which is to be used to help my testing team test the use of another service I have written, but in order to test the service I need a client.
Basically I have a list of tweets that I am displaying to the user, and I want them to be able to click on the tweet to find out the original raw payload that was sent via twitter.
my XAML is as follows:
<ItemsControl x:Name="ActivitiesList" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Black" Margin="2">
<Grid Height="100">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="50"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="300"/>
</Grid.ColumnDefinitions>
<Image Width="Auto" Height="Auto" Source="{Binding Avatar}" Grid.ColumnSpan="1" Grid.RowSpan="2" Grid.Column="0" Grid.Row="0" Margin="2,2,2,2" />
<TextBlock Text="{Binding Author}" Grid.ColumnSpan="3" Grid.RowSpan="1" Grid.Column="1" Grid.Row="0" />
<TextBlock Text="{Binding Id}" Grid.ColumnSpan="1" Grid.RowSpan="1" Grid.Column="3" Grid.Row="0" Margin="0,0,0,0" />
<Border BorderThickness="0" x:Name="border" Grid.ColumnSpan="3" Grid.RowSpan="1" Grid.Column="1" Grid.Row="1" Margin="0.5" />
<TextBlock Text="{Binding Body}" TextAlignment="Left" TextWrapping="Wrap" Grid.ColumnSpan="3" Grid.RowSpan="1" Grid.Column="1" Grid.Row="1" Width="{Binding ActualWidth, ElementName=border}" Height="{Binding ActualHeight, ElementName=border}" />
<TextBlock Text="Tags:" Grid.ColumnSpan="1" Grid.RowSpan="1" Grid.Column="0" Grid.Row="2" />
<ItemsControl ItemsSource="{Binding Tags}" Grid.ColumnSpan="2" Grid.Row="2" Grid.Column="1" Margin="0,0,0,0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1,1,1,1" BorderBrush="Black" CornerRadius="5" Margin="1,1,1,1" >
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFFEEEE" Offset="0"/>
<GradientStop Color="#FFFFEBEB" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<TextBlock Text="{Binding}" Margin="3,0,3,0" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Content="View Raw Data" Grid.Column="3" Grid.Row="2" HorizontalAlignment="Right" Width="105" Tag="{Binding}" Click="ButtonBase_OnClick" />
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<!--<local1:Tweet Id="tag:search.twitter.com,2005:823502379551248384 ( 1000 )" Author="jim parslow" Body="This is the content and is going to be very long so that it takes over the page more and more and more" Avatar="https://pbs.twimg.com/profile_images/539113272553648128/sWcgnCan_normal.jpeg" >
<local1:Tweet.Tags>
<System:String>Tag 1</System:String>
<System:String>Tag 2</System:String>
<System:String>Tag 3</System:String>
</local1:Tweet.Tags>
</local1:Tweet>
<local1:Tweet Id="tag:search.twitter.com,2005:823502379551248384 ( 1001 )" Author="jim parslow" Body="This is the content balh" Avatar="https://pbs.twimg.com/profile_images/539113272553648128/sWcgnCan_normal.jpeg" />
<local1:Tweet Id="tag:search.twitter.com,2005:823502379551248384 ( 1002 )" Author="jim parslow" Body="This is the content blah 2" Avatar="https://pbs.twimg.com/profile_images/539113272553648128/sWcgnCan_normal.jpeg" />-->
</ItemsControl>
I have added a button in and attached the entire datacontext item into the tag property of the button.
<Button Content="View Raw Data" Grid.Column="3" Grid.Row="2" HorizontalAlignment="Right" Width="105" Tag="{Binding}" Click="ButtonBase_OnClick" />
Then in the code I grab this out on button click:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var buttonClicked = (Button) sender;
var tag = buttonClicked.Tag;
Console.WriteLine("");
}
I have tried using a button with command, but can't get to work.
I have tried to use events from other controls and add mouse clicks on the grid, but again I cannot make this work.
Everything is currently in one xaml file and code file (I know!!)
The tweet class is below:
public class Tweet : INotifyPropertyChanged
{
public string Id { get; set; }
public string Author { get; set; }
public string Body { get; set; }
public string Avatar { get; set; }
public List<string> Tags { get; set; }
public Activity OriginalActivity { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
And everytime I get a new tweet I add it to the list and set the list:
ActivitiesList.ItemsSource = Tweets;
Where Tweets is an observable collection
private ObservableCollection<Tweet> _tweets = new ObservableCollection<Tweet>();
public ObservableCollection<Tweet> Tweets
{
get { return _tweets; }
}
if anyone could tell me the correct way to do this I would be eternally grateful.
EDIT* As asking for an example of how to do this properly wanted to know what I tried:
<Border.InputBindings>
<MouseBinding MouseAction="LeftClick" Command="{Binding DataContext.ShowData, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"/>
</Border.InputBindings>
I also tried this:
<Button Command="{Binding
RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}},
Path=DataContext.ShowData}" CommandParamater="{Binding}" />
But could never get anything to call the property on the tweet
public ICommand ShowData
{
get;
private set;
}
**
Another Edit: Why this no worky?
**
I have this button in the ItemsControl
<Button Content="View Raw Data" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.ShowDataCommand}" CommandParameter="{Binding}" />
And in the code I have the following:
public class Tweet : INotifyPropertyChanged
{
public string Id { get; set; }
public string Author { get; set; }
public string Body { get; set; }
public string Avatar { get; set; }
public List<string> Tags { get; set; }
public Activity OriginalActivity { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
RelayCommand _showData;
public ICommand ShowDataCommand
{
get
{
if (_showData == null)
{
_showData = new RelayCommand(param => this.ShowData((Tweet)param));
}
return _showData;
}
}
public void ShowData(Tweet tweet)
{
Debug.WriteLine("I AM HERE");
}
}
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute) : this(execute, null) { }
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute; _canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter) { _execute(parameter); }
#endregion // ICommand Members
}
Why does this not work?
I get no errors or messages that are saying I have anything wrong.
Can anyone help?