I have a datagrid and this gestures?
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding DatagridMouseDoubleClickCommand}" CommandParameter="{Binding ElementName=DataGrid, Path=SelectedItems, Mode=OneWay}"/>
<KeyBinding Key="Enter" Command="{Binding DataGridMouseDoubleClickCommand}" CommandParameter="{Binding ElementName=DataGrid, Path=SelectedItems, Mode=OneWay}"/>
</DataGrid.InputBindings>
I know that in a button, I can the property IsEnabled, but in the gestures in the data grid I don't have this property.
How can I disable the command of a gesture in the datagrid?
Related
I have a stack panel for which I need a mouse event to be handled in the ViewModel but the binding doesn't work - the command isn't found although the other elements are also binded to the ViewModel and they work. Here is the xaml:
<Window.DataContext>
<vm:Ticker/>
</Window.DataContext>
<ListView Grid.Row="3"
ItemsSource ="{Binding TickersCollectionView}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal"
Name="STPListView">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction
Command="{Binding MouseLeftButtonDownCommand}"
CommandParameter="{Binding ElementName=STPListView}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBlock
Width="130"
Text="{Binding Market}"
Foreground="WhiteSmoke"/>
<TextBlock
Width="110"
Text="{Binding Price}"
Foreground="{Binding LastPrice, Converter={StaticResource BoolToForeground}}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The command is a property of the ViewModel. How can I bind it?
If the MouseLeftButtonDownCommand property is defined in the same view model as the TickersCollectionView property, you should bind to it using a RelativeSource:
Command="{Binding DataContext.MouseLeftButtonDownCommand,
RelativeSource={RelativeSource AncestorType=ListView}}"
I am learning wpf and mvvm and I decided to create a Soundboard for myself to practice and so far it's going pretty well.
Now I have made a datatemplate where for every file that the program finds in the specified directory it will create a button with the name of the file in it and I can click it to play. So far so good.
However I now tried to make a ContextMenu so that when I want to remove a file from the list I can right click and select remove, but this command doesn't work even though I have the exact same command structure for the regular button.
I am really quite confused with the whole RelativeSource thing and was already happy my regular 'play' command worked in the button.
If someone could point me in the right direction that would be great. I really could use an explanation on my specific problem as that always seems to help me more then a generic example somehow. I have tried to read on all the related questions but just don't seem to figure it out from there.
My ItemsControl:
<ItemsControl x:Name="MySounds" ItemsSource="{Binding Sounds}">
ItemTemplate:
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Style="{StaticResource mainButton}"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.PlaySound}"
CommandParameter="{Binding Path=Tag, RelativeSource={RelativeSource Self}}"
Tag="{Binding Path=Name}">
<TextBlock Text="{Binding Path=NormalizedName}" TextWrapping="Wrap" Height="auto" />
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="{Binding Path=Name}"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.RemoveSound}"
CommandParameter="{Binding Path=Tag, RelativeSource={RelativeSource Self}}">
<MenuItem.Icon>
<Image Source="\WpfPractice;component\Images\CoffeeArt.png" Width="20" VerticalAlignment="Center"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
I have a generic RelayCommand in my viewmodel and that all works, the problem really is just with the binding.
If you bind the Tag property of the Button to the ItemsControl, you could bind to the command using the PlacementTarget property of the ContextMenu:
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Style="{StaticResource mainButton}"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.PlaySound}"
CommandParameter="{Binding Path=Name}"
Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}">
<TextBlock Text="{Binding Path=NormalizedName}" TextWrapping="Wrap" Height="auto" />
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="{Binding Path=Name}"
Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Tag.DataContext.RemoveSound}"
CommandParameter="{Binding Path=Name}">
<MenuItem.Icon>
<Image Source="\WpfPractice;component\Images\CoffeeArt.png" Width="20" VerticalAlignment="Center"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
You can try to replace your command string in your MenuItem by this :
Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.RemoveSound}"
I have a problem. How to bind a ItemsSource property to a NumericUpDown? This way it does not work. Thx!
<DataGrid ItemsSource="{Binding Articles}">
<DataGrid.Columns>
<DataGridTemplateColumn MinWidth="100"
Header="Amount"
MaxWidth="{Binding MinWidth, RelativeSource={RelativeSource Self}}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<mahApps:NumericUpDown Value="{Binding Amount, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="ValueChanged">
<i:InvokeCommandAction CommandParameter="{Binding}"
Command="{Binding DataContext.RefreshValuesCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</mahApps:NumericUpDown>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
EDIT
FIRST: Cantidad (Spanish) = Amount (English)
If the Amount property is defined in the same class as the Articles property you should bind to the DataContext (DataContext.Amount) of the DataGrid:
<mahApps:NumericUpDown Value="{Binding DataContext.Amount, UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="ValueChanged">
<i:InvokeCommandAction CommandParameter="{Binding}"
Command="{Binding DataContext.RefreshValuesCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</mahApps:NumericUpDown>
If the Amount property is defined in the data object (Article or whatever you call it) class it is simply:
<mahApps:NumericUpDown Value="{Binding Amount}">
Edit: You should also set the UpdateSourceTrigger of the Binding to PropertyChanged:
<mahApps:NumericUpDown Value="{Binding Amount, UpdateSourceTrigger=PropertyChanged}" />
I have an wpf application.I want to show the selected item in the Combo box.
I get an error saying Cant use both DisplayMemberPath and Item Template.
My ItemsSource is not of string type
its a class called "StockExchange"
Following is my code :
<telerik:RadComboBox Grid.Column="1" DisplayMemberPath="StockExchangeName" Name="cmbStockExchange" Foreground="White" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="118,14,0,0" VerticalAlignment="Top" Width="100" Height="23" ItemsSource="{Binding StockExchange, Mode=TwoWay}" SelectedItem="{Binding SelectedStockExchange,Mode= TwoWay}" telerik:StyleManager.Theme="Summer" TabIndex="3">
<telerik:RadComboBox.ItemTemplate >
<DataTemplate>
<CheckBox Name="StockExchange" Content="{Binding StockExchangeName}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<Commands:EventToCommand Command="{Binding DataContext.StockExchangeCheckedCmd,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:RadWindow}}}" CommandParameter="{Binding ElementName=StockExchange}" ></Commands:EventToCommand>
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<Commands:EventToCommand Command="{Binding DataContext.StockExchangeUnCheckedCmd,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:RadWindow}}}" CommandParameter="{Binding ElementName=StockExchange}" ></Commands:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</DataTemplate>
</telerik:RadComboBox.ItemTemplate>
</telerik:RadComboBox>
what is solution for this? how can I display single or multiple selected Items in a Combo box?
<telerik:RadComboBox Grid.Column="1" Name="cmbStockExchange" Foreground="White" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="118,14,0,0" VerticalAlignment="Top" Width="100" Height="23" ItemsSource="{Binding StockExchange, Mode=TwoWay}" SelectedItem="{Binding SelectedStockExchange,Mode= TwoWay}" telerik:StyleManager.Theme="Summer" TabIndex="3">
<telerik:RadComboBox.ItemTemplate >
<DataTemplate>
<CheckBox Name="StockExchange" Content="{Binding StockExchangeName}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<Commands:EventToCommand Command="{Binding DataContext.StockExchangeCheckedCmd,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:RadWindow}}}" CommandParameter="{Binding ElementName=StockExchange}" ></Commands:EventToCommand>
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<Commands:EventToCommand Command="{Binding DataContext.StockExchangeUnCheckedCmd,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:RadWindow}}}" CommandParameter="{Binding ElementName=StockExchange}" ></Commands:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</DataTemplate>
</telerik:RadComboBox.ItemTemplate>
Create a Textblock that Shows the selected item StockExchangeName
<TextBlock Text="{Binding Path=SelectedItem.StockExchangeName, ElementName=cmbStockExchange}" />
If you select CheckBox, no item is shown selected in comboBox because no item is selected since click event is handled by checkbox.
You can bind IsChecked with IsSelected value of ComboBoxItem so that on click of checkbox corresponding item gets selected.
<CheckBox Content="{Binding StockExchangeName}"
IsChecked="{Binding IsSelected, RelativeSource={RelativeSource
Mode=FindAncestor, AncestorType=ComboBoxItem}}"/>
This will show checkBox in comboBox because you have provided template with checkBox in it for comboBoxItem.
I have implemented a Label in DataGrid using MVVM. My requirement is when user clicks on the Label the events need to be raised which is working fine. But now I would like to capture the label text, but I am unable to acheive this:
<DataGrid HorizontalAlignment="Stretch" Name="DgPreviousEntries" HeadersVisibility="None"
ItemsSource="{Binding WeeklyWiseEntries}" AutoGenerateColumns="False" SelectedItem="{Binding SelectedweekEntry}">
<DataGrid.Columns>
<DataGridTemplateColumn IsReadOnly="True" Width="600">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<Grid>
<Label Grid.Column="0" Grid.Row="1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.WeekCommand}">
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
<Label.Content>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="Mon
{0}">
<Binding Path="Monday" Source="Monday" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Label.Content>
</Label>
</Grid>
</StackPanel>
<DataTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataGrid.Columns>
</DataGrid>
Did you try passing the Label Text as Command Paramter?
CommandParameter={Binding Path=Content, ElementName=LabelName}
E.g:
<Label Grid.Column="0" Grid.Row="1" Name="DayLabel">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction
Command="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.WeekCommand}"
CommandParameter={Binding Path=Content, ElementName=DayLabel}>
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
<Label.Content>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="Mon
{0}">
<Binding Path="Monday" Source="Monday" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Label.Content>
</Label>