Replies
Is there a way to do this using RelativeSource? I was hoping not to have to change the way I bind the view to the viewmodel.
I am having a similar issue except with a combobox instead of a checkbox.
Any help greatly appreciated.
Here is my code:
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:igDP="http://infragistics.com/DataPresenter"
xmlns:wpfApplication1="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<wpfApplication1:Value1VisibilityConverter x:Key="Value1VisibilityConverter" />
<wpfApplication1:Value2VisibilityConverter x:Key="Value2VisibilityConverter" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0" Margin="10 0 0 10" >
<TextBlock VerticalAlignment="Center">Which Value?</TextBlock>
<ComboBox Width="80" Margin="10 0 0 0" SelectedIndex="{Binding SelectedIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
<ComboBoxItem>Value 1</ComboBoxItem>
<ComboBoxItem>Value 2</ComboBoxItem>
</ComboBox>
</StackPanel>
<igDP:XamDataGrid VerticalAlignment="Top"
Grid.Row="1"
AutoFit="True"
GroupByAreaLocation="None"
Theme="Office2013"
DataSource="{Binding GridValues}">
<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings AutoGenerateFields="False"/>
</igDP:XamDataGrid.FieldLayoutSettings>
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:UnboundField Label="Product" Binding="{Binding ProductName}"/>
<igDP:UnboundField Label="Value1" Width="100" Binding="{Binding Value1, Mode=TwoWay}" Visibility="{Binding SelectedIndex, Converter={StaticResource Value1VisibilityConverter}, UpdateSourceTrigger=PropertyChanged}"/>
<igDP:UnboundField Label="Value2" Width="100" Binding="{Binding Value2, Mode=TwoWay}" Visibility="{Binding SelectedIndex, Converter={StaticResource Value2VisibilityConverter}, UpdateSourceTrigger=PropertyChanged}"/>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyVM vm = new MyVM();
this.DataContext = vm;
vm.GridValues = new ObservableCollection<GridRow>();
vm.GridValues.Add(new GridRow() { ProductName = "Product 1", Value1 = 1, Value2 = 0 });
vm.GridValues.Add(new GridRow() { ProductName = "Product 2", Value1 = 1, Value2 = 0 });
vm.GridValues.Add(new GridRow() { ProductName = "Product 3", Value1 = 1, Value2 = 0 });
vm.GridValues.Add(new GridRow() { ProductName = "Product 4", Value1 = 1, Value2 = 0 });
vm.GridValues.Add(new GridRow() { ProductName = "Product 5", Value1 = 1, Value2 = 0 });
vm.RaisePropertyChanged("GridValues");
}
}
public class ViewModelBase : INotifyPropertyChanged
{
public void RaisePropertyChanged(String property)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(property));
}
public event PropertyChangedEventHandler PropertyChanged = null;
}
public class MyVM : ViewModelBase
{
private int _selectedIndex;
public int SelectedIndex
{
get { return _selectedIndex; }
set
{
_selectedIndex = value;
this.RaisePropertyChanged("SelectedIndex");
}
}
public ObservableCollection<GridRow> GridValues { get; set; }
}
public class GridRow : ViewModelBase
{
public String ProductName { get; set; }
public Double Value1 { get; set; }
public Double Value2 { get; set; }
}
internal class Value1VisibilityConverter : IValueConverter
{
#region IValueConverter Members
public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
{
return ((int)value) == 0 ? "Visible" : "Collapsed";
}
public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
{
return value;
}
#endregion
}
internal class Value2VisibilityConverter : IValueConverter
{
#region IValueConverter Members
public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
{
return ((int)value) == 1 ? "Visible" : "Collapsed";
}
public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
{
return value;
}
#endregion
}
}