Binding gives UnsetValue on multiple binding - c#

I have a datagrid which gets its data out of a datatable like
Inside if there is a textbox with a contextmenu and this contextmenu has a converter (with multibinding)
The problem is i datatable the binded data has 1 or 0 as data but in the converter i get UnsetValue.
This is the xaml code:
<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" VirtualizingStackPanel.VirtualizationMode="Recycling" VirtualizingStackPanel.IsVirtualizing="True" Margin="0,1,0,0" x:Name="TrackingDataGrid" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False" IsManipulationEnabled="True" CanUserResizeColumns="False" ItemsSource="{Binding TrackingTable}" ColumnHeaderStyle="{DynamicResource dataGridColumnStyle}" Style="{DynamicResource dataGridStyle}" EnableColumnVirtualization="True" IsEnabled="True" BorderThickness="0" CellStyle="{DynamicResource CellStyleBase}" VerticalGridLinesBrush="#FFA0A7AD" RowHeight="30" MinRowHeight="20" GridLinesVisibility="None" SelectionMode="Single" Grid.Row="1" Grid.ColumnSpan="10" Panel.ZIndex="4" MinHeight="30" Loaded="TrackingDataGrid_Loaded" CanUserReorderColumns="False" VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Process Time" IsReadOnly="True" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DeliveryDate}" TextTrimming="CharacterEllipsis" >
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem x:Name="StandbyProcessMenuITem" Header="StandBy Process" Click="StandbyProcessMenuITem_Click" >
<!--<MenuItem.Visibility>
<MultiBinding Converter="{StaticResource RowUndoButtonVisibility}">
<Binding Path="ProductIsStandby" />
<Binding Path="ProductIsDone" />
</MultiBinding>
</MenuItem.Visibility>-->
</MenuItem>
<MenuItem x:Name="UndoProcessMenuITem" Header="Undo Process" Click="UndoProcessMenuITem_Click" >
<MenuItem.Visibility>
<MultiBinding Converter="{StaticResource RowUndoButtonVisibility}">
<Binding Path="{Binding ProductIsStandby}" />
<Binding Path="{Binding ProductIsDone}" />
</MultiBinding>
</MenuItem.Visibility>
</MenuItem>
<MenuItem x:Name="FinishProcessMenuITem" Header="Finish Process" Click="FinishProcessMenuITem_Click">
</MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

The DataContext of a MenuItem is not the same as the DataContext of the CellTemplate but you could use a RelativeSource to bind to the PlacementTarget of the ContextMenu:
<MenuItem x:Name="UndoProcessMenuITem" Header="Undo Process" Click="UndoProcessMenuITem_Click" >
<MenuItem.Visibility>
<MultiBinding Converter="{StaticResource RowUndoButtonVisibility}">
<Binding Path="{Binding PlacementTarget.DataContext.ProductIsStandby, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
<Binding Path="{Binding PlacementTarget.DataContext.ProductIsDone, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
</MultiBinding>
</MenuItem.Visibility>
</MenuItem>

Related

SelectionChanged event of datagrid is not firing in mvvm way

//I am having a Nested datagrid
and i need to fire the event when inner grid row selection changes.
<DataGrid ScrollViewer.CanContentScroll="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
HeadersVisibility="Row"
Height="{Binding ElementName=itemsgrid,Path=ActualHeight}"
Grid.Row="1"
RowDetailsVisibilityMode="Visible"
CanUserAddRows="false"
VerticalAlignment="Top"
AutoGenerateColumns="False"
SelectedItem="{Binding SelectedBasketItem}"
ItemsSource="{Binding SelectedOrderBasketItems}"
CanUserDeleteRows="True">
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid ScrollViewer.CanContentScroll="True"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
CanUserAddRows="false"
HeadersVisibility="Row"
AutoGenerateColumns="False"
ItemsSource="{Binding SelectedBasketItems}"
SelectedValue="{Binding SelectedBasketItemValue}"
SelectedIndex="{Binding SelectedOrderItemIndex}"
SelectedItem="{Binding SelectedSpecificItemInBasket, Mode=TwoWay}"
DataGrid.SelectionMode="Single"
Style="{StaticResource GridItemsControlStyle}"
ctrls:DragDrop.DropHandler="{Binding DropHandler}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding DataContext.DgSelectionChangedCommand,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True"
Binding="{Binding Path=ItemName}"
Width="195">
</DataGridTextColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Path=RulesCount}"
Background="{Binding Path=RulesCount ,Converter={StaticResource ItemColourConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="115">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Path=LinkedOrderCount}"
Background="{Binding Path=LinkedOrderCount ,Converter={StaticResource ItemColourConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="55">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Path=SDICount}">
<Label.Background>
<MultiBinding Converter="{StaticResource SdiItemColourConverter}">
<Binding ElementName="SDIMaxCnt"
Path="Value" />
<Binding Path="SDICount" />
</MultiBinding>
</Label.Background>
</Label>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
//My Delegate Command initialization in ViewModel
protected override void InitializeCommands()
{
DgSelectionChangedCommand= new DelegateCommand<object>(DGSelectionChanged);
}
//My Method in the view model to be called when selection changed event is fired
void DGSelectionChanged(object obj)
{
//Logic
}
The event gets fired when i use the same event in code behind . i am trying to use interaction trigger to achieve the same in mvvm way .Not sure what i am missing .Any help is greatly appreciated.
Set the AncestorLevel of the RelativeSource to 2:
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding DataContext.DgSelectionChangedCommand,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, AncestorLevel=2}}"
CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
Autopostback is the mechanism, by which the page will be posted back to the server automatically based on some events in the web controls. In some of the web controls, the property called auto post back.
which if set to true, will send the request to the server when an event happens in the control.
AutoPostBack=True;
please go through the link for more:
Difference between AutoPostBack=True and AutoPostBack=False?

WPF Datagrid Column Visibility not working with MultiBinding

I am trying to control the visibility of a DataGridTextColumn using two Boolean values. One of the values is being set by a combo box and I am able to see in the debugger that Boolean values are being properly handled by the booleanAllTrueConverter.
My problem is that the converter value being returned has no effect on the column visibility. The column is always visible even when FALSE is returned from the converter.
I would certainly appreciate any ideas that are offered.
Thanks.
<DataGrid
Name="myDataGrid"
ItemsSource="{Binding Source={StaticResource ZZZZ}}"
AutoGenerateColumns="False"
IsReadOnly="True"
KeyboardNavigation.TabNavigation="None"
>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding SomeItems}" >
<DataGridTextColumn.Visibility>
<MultiBinding Converter="{StaticResource booleanAllTrueConverter}">
<Binding
Source="{StaticResource ProxyElement}"
Path="DataContext.IsBlahVisible"
Mode="TwoWay"
UpdateSourceTrigger="PropertyChanged"
/>
<Binding
Source="{StaticResource ProxyElement}"
Path="DataContext.AreColumnsVisible"
Mode="TwoWay"
UpdateSourceTrigger="PropertyChanged"
/>
</MultiBinding>
</DataGridTextColumn.Visibility>
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<local:ComboBox
Grid.Column="0"
Width="22"
Height="36"
HorizontalAlignment="Right"
VerticalAlignment="Center"
ItemsSource="{Binding DataContext.SomeItems, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
SelectedItems="{Binding DataContext.SelectedSomeItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
></local:ComboBox>
<TextBlock
Text="SomeItems"
Grid.Column="1"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Grid.ColumnSpan="2"
Height="16"
Margin="0,3"
Width="37"
/>
</Grid>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
You need to return either Visibility.Visible instead of true or return Visibility.Collapsed instead of false from booleanAllTrueConverter

DependencyProperty.UnsetValue using template and MultiBinding

Anyone knows why I receive the error: "DependencyProperty.UnsetValue" when I call my Command through the Template
This is my template:
<DataTemplate x:Key="MenuComboBoxItemTemplate" DataType="ComboBox">
<DockPanel>
<TextBlock DockPanel.Dock="Left" Text="{Binding Text.Display}" />
<Button x:Name="RemoveButton"
Style="{StaticResource DeleteButton}"
DockPanel.Dock="Right"
ToolTip="Delete"
HorizontalAlignment="Right"
Padding="2"
Margin="3,0,0,0"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl},AncestorLevel=1}, Path=DataContext.RemoveMenuItemCommand}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource MultiValueConverter}">
<Binding Path="Name" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type menus:MenuComboBox}}" />
<Binding />
</MultiBinding>
</Button.CommandParameter>
<Image Source="{dx:DXImageOffice2013 Image=Delete_16x16.png}" />
</Button>
</DockPanel>
</DataTemplate>
My combobox:
<menus:MenuComboBox
x:Name="MyItems"
Grid.Column="0"
Grid.Row="2"
Padding="6,3,5,3"
BorderThickness="1"
Text="{Binding MyItems, UpdateSourceTrigger=LostFocus}"
ItemTemplate="{StaticResource MenuComboBoxItemTemplate}"
ItemsSource="{Binding Menus[MyItems].Items}"
NewMenuItemCommand="{Binding AddMenuItemCommand}"
GotFocusCommand="{Binding GotFocusCommand}" />
I am stuck on it :(

MultiBinding and ContentPresenter

This is my code of ContentPresenter:
<ListBox ItemsSource="{Binding Items}" BorderThickness="0" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<ContentPresenter x:Name="PART_ValueContainer">
<ContentPresenter.Content>
<MultiBinding>
<Binding Path="Value"/>
<Binding Path="ReadOnly"/>
</MultiBinding>
</ContentPresenter.Content>
<ContentPresenter.Resources>
<DataTemplate>
<TextBox IsReadOnly="{Binding Path=Content.ReadOnly, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"
Text="{Binding Path=Content.Value, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"
TextAlignment="Left"/>
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I trying to fill the Text from object Items.Value and property IsReadOnly from Items.ReadOnly.
It doesn't work, I know that is not good solution, but, how to make something like that?

input control in datagrid

how can you control what the user inputs in datagrid.
i need to be able to control input of each DataGridTextColumn.
if possible i want to be able to control the input when a character is typed in the datagrid. so selectionchanged for listbox.
sadly what i have now isn't working.
<DataGrid AutoGenerateColumns="False" Grid.Row="1" ItemsSource="{Binding ContactList}" SelectedItem="{Binding SelectedContact,UpdateSourceTrigger=PropertyChanged}" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Width="25" Binding="{Binding ID}" IsReadOnly="True"/>
<DataGridTextColumn Header="Naam" Width="1*" Binding="{Binding Name}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<cmd:EventToCommand Command="{Binding TestCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGridTextColumn>
<DataGridTextColumn Header="Bedrijf" Width="1*" Binding="{Binding Company}"/>
<DataGridTemplateColumn Width="1*" Header="Functie">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding JobRoleList}" DisplayMemberPath="Name" SelectedItem="{Binding JobRole.Name,UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="ID" SelectedValue="{Binding JobRole.ID,UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
thanks
edit i get an XamlParceException
xaml
<DataGridTemplateColumn Header="Naam" CellTemplate="{StaticResource ContactNameTemplate}" CellEditingTemplate="{StaticResource EditingContactNameTemplate}" />
dictionary
<DataTemplate x:Key="ContactNameTemplate" >
<TextBox HorizontalAlignment="Center">
<TextBox.Text>
<Binding Path="Name" Source="{Binding Name}" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<c:Text Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</DataTemplate>
<DataTemplate x:Key="EditingContactNameTemplate">
<TextBox HorizontalAlignment="Center">
<TextBox.Text>
<Binding Path="Name" Source="{Binding Name}" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<c:Text Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</DataTemplate>

Categories

Resources