Placing a context menu on the last Data Column of Data Grid to control the visibility of other columns in WPF
How can I place a context menu on the last Data Column of DataGrid such that it can control the visibility of other columns.
I have a Data grid with total 8 columns. How can i place a context menu on the last column ?
Need to understand if there exist is any possibility to control the visibility of other columns from the context menu (as shown in image below) ?
<DataGrid x:Name="MyGrid" ItemsSource="{Binding obvCollection}" AutoGenerateColumns="False" >
<DataGridTemplateColumn Header = "Column 1"/>
<DataGridTemplateColumn Header = "Column 2"/>
<DataGridTemplateColumn Header = "Column 3"/>
<DataGridTemplateColumn Header = "Column 4"/>
<DataGridTemplateColumn Header = "Column 5"/>
<DataGridTemplateColumn Header = "Column 6"/>
<DataGridTemplateColumn Header = "Column 7"/>
<DataGridTemplateColumn Header = "Column 8"/>
<DataGridTemplateColumn Header = ""/>
</Datagrid>
c# wpf wpf-controls contextmenu wpfdatagrid
add a comment |
How can I place a context menu on the last Data Column of DataGrid such that it can control the visibility of other columns.
I have a Data grid with total 8 columns. How can i place a context menu on the last column ?
Need to understand if there exist is any possibility to control the visibility of other columns from the context menu (as shown in image below) ?
<DataGrid x:Name="MyGrid" ItemsSource="{Binding obvCollection}" AutoGenerateColumns="False" >
<DataGridTemplateColumn Header = "Column 1"/>
<DataGridTemplateColumn Header = "Column 2"/>
<DataGridTemplateColumn Header = "Column 3"/>
<DataGridTemplateColumn Header = "Column 4"/>
<DataGridTemplateColumn Header = "Column 5"/>
<DataGridTemplateColumn Header = "Column 6"/>
<DataGridTemplateColumn Header = "Column 7"/>
<DataGridTemplateColumn Header = "Column 8"/>
<DataGridTemplateColumn Header = ""/>
</Datagrid>
c# wpf wpf-controls contextmenu wpfdatagrid
Last column should DataGridTemplateColumn->Control->ContextMenu->ItemsControl->Checkboxes->Bind-To-Visibiity.
– Kaspar
Nov 22 '18 at 10:00
add a comment |
How can I place a context menu on the last Data Column of DataGrid such that it can control the visibility of other columns.
I have a Data grid with total 8 columns. How can i place a context menu on the last column ?
Need to understand if there exist is any possibility to control the visibility of other columns from the context menu (as shown in image below) ?
<DataGrid x:Name="MyGrid" ItemsSource="{Binding obvCollection}" AutoGenerateColumns="False" >
<DataGridTemplateColumn Header = "Column 1"/>
<DataGridTemplateColumn Header = "Column 2"/>
<DataGridTemplateColumn Header = "Column 3"/>
<DataGridTemplateColumn Header = "Column 4"/>
<DataGridTemplateColumn Header = "Column 5"/>
<DataGridTemplateColumn Header = "Column 6"/>
<DataGridTemplateColumn Header = "Column 7"/>
<DataGridTemplateColumn Header = "Column 8"/>
<DataGridTemplateColumn Header = ""/>
</Datagrid>
c# wpf wpf-controls contextmenu wpfdatagrid
How can I place a context menu on the last Data Column of DataGrid such that it can control the visibility of other columns.
I have a Data grid with total 8 columns. How can i place a context menu on the last column ?
Need to understand if there exist is any possibility to control the visibility of other columns from the context menu (as shown in image below) ?
<DataGrid x:Name="MyGrid" ItemsSource="{Binding obvCollection}" AutoGenerateColumns="False" >
<DataGridTemplateColumn Header = "Column 1"/>
<DataGridTemplateColumn Header = "Column 2"/>
<DataGridTemplateColumn Header = "Column 3"/>
<DataGridTemplateColumn Header = "Column 4"/>
<DataGridTemplateColumn Header = "Column 5"/>
<DataGridTemplateColumn Header = "Column 6"/>
<DataGridTemplateColumn Header = "Column 7"/>
<DataGridTemplateColumn Header = "Column 8"/>
<DataGridTemplateColumn Header = ""/>
</Datagrid>
c# wpf wpf-controls contextmenu wpfdatagrid
c# wpf wpf-controls contextmenu wpfdatagrid
edited Nov 22 '18 at 14:28
user42067
asked Nov 22 '18 at 9:30
user42067user42067
113214
113214
Last column should DataGridTemplateColumn->Control->ContextMenu->ItemsControl->Checkboxes->Bind-To-Visibiity.
– Kaspar
Nov 22 '18 at 10:00
add a comment |
Last column should DataGridTemplateColumn->Control->ContextMenu->ItemsControl->Checkboxes->Bind-To-Visibiity.
– Kaspar
Nov 22 '18 at 10:00
Last column should DataGridTemplateColumn->Control->ContextMenu->ItemsControl->Checkboxes->Bind-To-Visibiity.
– Kaspar
Nov 22 '18 at 10:00
Last column should DataGridTemplateColumn->Control->ContextMenu->ItemsControl->Checkboxes->Bind-To-Visibiity.
– Kaspar
Nov 22 '18 at 10:00
add a comment |
2 Answers
2
active
oldest
votes
You could implement this with a converter:
class VisibilityToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Visibility visibility = (Visibility)value;
return visibility == Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool b = (bool)value;
return b ? Visibility.Visible : Visibility.Hidden;
}
}
...and some XAML markup:
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<Grid Background="Gray" Width="25" Height="25"
Tag="{Binding Columns, ElementName=MyGrid}">
<Grid.ContextMenu>
<ContextMenu ItemsSource="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="chk">
<CheckBox.IsChecked>
<Binding Path="Visibility">
<Binding.Converter>
<local:VisibilityToBooleanConverter />
</Binding.Converter>
</Binding>
</CheckBox.IsChecked>
<CheckBox.Content>
<TextBlock Text="{Binding Header}" />
</CheckBox.Content>
</CheckBox>
<DataTemplate.Triggers>
<!-- hide the last column in the ContentMenu-->
<DataTrigger Binding="{Binding DisplayIndex}" Value="5">
<Setter TargetName="chk" Property="Visibility" Value="Collapsed" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</DataGridTemplateColumn.Header>
</DataGridTemplateColumn>
Replace the Grid
in the sample markup with a hamburger button or whatever element you want the user to click on to bring up the ContextMenu
.
It's not working. Clicking on Grid or button doesn't opens the context menu
– user42067
Nov 22 '18 at 12:13
You should right click on it to open the ContextMenu. That's how ContextMenus work.
– mm8
Nov 22 '18 at 12:24
add a comment |
You could use something along the lines of:
<Expander Grid.Column="0" ExpandDirection="Right" Width="auto" IsExpanded="false" Header="Options" Background="#ececec" Padding="5">
<StackPanel>
<Label Content="Columns:" FontWeight="SemiBold" />
<ItemsControl ItemsSource="{Binding ElementName=DataGridName, Path=Columns}" Grid.IsSharedSizeScope="True" Margin="5">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A"/>
<ColumnDefinition SharedSizeGroup="B"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Header}" Margin="20, 5, 5, 0"/>
<CheckBox Grid.Column="1" IsChecked="{Binding Visibility, Converter=cnv:CustomVisibilityToBool CollapsedValue=False, VisibleValue=True}}"
Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Expander>
Converter:
class CustomVisibilityToBool : MarkupExtension, IValueConverter
{
public bool VisibleValue { get; set; }
public bool CollapsedValue { get; set; }
public bool HiddenValue { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((value as Visibility?) == Visibility.Visible) return VisibleValue;
else if ((value as Visibility?) == Visibility.Hidden) return HiddenValue;
else if ((value as Visibility?) == Visibility.Collapsed) return CollapsedValue;
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((value as bool?) == VisibleValue) return Visibility.Visible;
else if ((value as bool?) == HiddenValue) return Visibility.Hidden;
else if ((value as bool?) == CollapsedValue) return Visibility.Collapsed;
return Binding.DoNothing;
}
}
Which if you place next to the DataGrid
will look like:
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427710%2fplacing-a-context-menu-on-the-last-data-column-of-data-grid-to-control-the-visib%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could implement this with a converter:
class VisibilityToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Visibility visibility = (Visibility)value;
return visibility == Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool b = (bool)value;
return b ? Visibility.Visible : Visibility.Hidden;
}
}
...and some XAML markup:
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<Grid Background="Gray" Width="25" Height="25"
Tag="{Binding Columns, ElementName=MyGrid}">
<Grid.ContextMenu>
<ContextMenu ItemsSource="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="chk">
<CheckBox.IsChecked>
<Binding Path="Visibility">
<Binding.Converter>
<local:VisibilityToBooleanConverter />
</Binding.Converter>
</Binding>
</CheckBox.IsChecked>
<CheckBox.Content>
<TextBlock Text="{Binding Header}" />
</CheckBox.Content>
</CheckBox>
<DataTemplate.Triggers>
<!-- hide the last column in the ContentMenu-->
<DataTrigger Binding="{Binding DisplayIndex}" Value="5">
<Setter TargetName="chk" Property="Visibility" Value="Collapsed" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</DataGridTemplateColumn.Header>
</DataGridTemplateColumn>
Replace the Grid
in the sample markup with a hamburger button or whatever element you want the user to click on to bring up the ContextMenu
.
It's not working. Clicking on Grid or button doesn't opens the context menu
– user42067
Nov 22 '18 at 12:13
You should right click on it to open the ContextMenu. That's how ContextMenus work.
– mm8
Nov 22 '18 at 12:24
add a comment |
You could implement this with a converter:
class VisibilityToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Visibility visibility = (Visibility)value;
return visibility == Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool b = (bool)value;
return b ? Visibility.Visible : Visibility.Hidden;
}
}
...and some XAML markup:
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<Grid Background="Gray" Width="25" Height="25"
Tag="{Binding Columns, ElementName=MyGrid}">
<Grid.ContextMenu>
<ContextMenu ItemsSource="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="chk">
<CheckBox.IsChecked>
<Binding Path="Visibility">
<Binding.Converter>
<local:VisibilityToBooleanConverter />
</Binding.Converter>
</Binding>
</CheckBox.IsChecked>
<CheckBox.Content>
<TextBlock Text="{Binding Header}" />
</CheckBox.Content>
</CheckBox>
<DataTemplate.Triggers>
<!-- hide the last column in the ContentMenu-->
<DataTrigger Binding="{Binding DisplayIndex}" Value="5">
<Setter TargetName="chk" Property="Visibility" Value="Collapsed" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</DataGridTemplateColumn.Header>
</DataGridTemplateColumn>
Replace the Grid
in the sample markup with a hamburger button or whatever element you want the user to click on to bring up the ContextMenu
.
It's not working. Clicking on Grid or button doesn't opens the context menu
– user42067
Nov 22 '18 at 12:13
You should right click on it to open the ContextMenu. That's how ContextMenus work.
– mm8
Nov 22 '18 at 12:24
add a comment |
You could implement this with a converter:
class VisibilityToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Visibility visibility = (Visibility)value;
return visibility == Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool b = (bool)value;
return b ? Visibility.Visible : Visibility.Hidden;
}
}
...and some XAML markup:
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<Grid Background="Gray" Width="25" Height="25"
Tag="{Binding Columns, ElementName=MyGrid}">
<Grid.ContextMenu>
<ContextMenu ItemsSource="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="chk">
<CheckBox.IsChecked>
<Binding Path="Visibility">
<Binding.Converter>
<local:VisibilityToBooleanConverter />
</Binding.Converter>
</Binding>
</CheckBox.IsChecked>
<CheckBox.Content>
<TextBlock Text="{Binding Header}" />
</CheckBox.Content>
</CheckBox>
<DataTemplate.Triggers>
<!-- hide the last column in the ContentMenu-->
<DataTrigger Binding="{Binding DisplayIndex}" Value="5">
<Setter TargetName="chk" Property="Visibility" Value="Collapsed" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</DataGridTemplateColumn.Header>
</DataGridTemplateColumn>
Replace the Grid
in the sample markup with a hamburger button or whatever element you want the user to click on to bring up the ContextMenu
.
You could implement this with a converter:
class VisibilityToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Visibility visibility = (Visibility)value;
return visibility == Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool b = (bool)value;
return b ? Visibility.Visible : Visibility.Hidden;
}
}
...and some XAML markup:
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header>
<Grid Background="Gray" Width="25" Height="25"
Tag="{Binding Columns, ElementName=MyGrid}">
<Grid.ContextMenu>
<ContextMenu ItemsSource="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<CheckBox x:Name="chk">
<CheckBox.IsChecked>
<Binding Path="Visibility">
<Binding.Converter>
<local:VisibilityToBooleanConverter />
</Binding.Converter>
</Binding>
</CheckBox.IsChecked>
<CheckBox.Content>
<TextBlock Text="{Binding Header}" />
</CheckBox.Content>
</CheckBox>
<DataTemplate.Triggers>
<!-- hide the last column in the ContentMenu-->
<DataTrigger Binding="{Binding DisplayIndex}" Value="5">
<Setter TargetName="chk" Property="Visibility" Value="Collapsed" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</DataGridTemplateColumn.Header>
</DataGridTemplateColumn>
Replace the Grid
in the sample markup with a hamburger button or whatever element you want the user to click on to bring up the ContextMenu
.
answered Nov 22 '18 at 10:31
mm8mm8
84.9k81932
84.9k81932
It's not working. Clicking on Grid or button doesn't opens the context menu
– user42067
Nov 22 '18 at 12:13
You should right click on it to open the ContextMenu. That's how ContextMenus work.
– mm8
Nov 22 '18 at 12:24
add a comment |
It's not working. Clicking on Grid or button doesn't opens the context menu
– user42067
Nov 22 '18 at 12:13
You should right click on it to open the ContextMenu. That's how ContextMenus work.
– mm8
Nov 22 '18 at 12:24
It's not working. Clicking on Grid or button doesn't opens the context menu
– user42067
Nov 22 '18 at 12:13
It's not working. Clicking on Grid or button doesn't opens the context menu
– user42067
Nov 22 '18 at 12:13
You should right click on it to open the ContextMenu. That's how ContextMenus work.
– mm8
Nov 22 '18 at 12:24
You should right click on it to open the ContextMenu. That's how ContextMenus work.
– mm8
Nov 22 '18 at 12:24
add a comment |
You could use something along the lines of:
<Expander Grid.Column="0" ExpandDirection="Right" Width="auto" IsExpanded="false" Header="Options" Background="#ececec" Padding="5">
<StackPanel>
<Label Content="Columns:" FontWeight="SemiBold" />
<ItemsControl ItemsSource="{Binding ElementName=DataGridName, Path=Columns}" Grid.IsSharedSizeScope="True" Margin="5">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A"/>
<ColumnDefinition SharedSizeGroup="B"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Header}" Margin="20, 5, 5, 0"/>
<CheckBox Grid.Column="1" IsChecked="{Binding Visibility, Converter=cnv:CustomVisibilityToBool CollapsedValue=False, VisibleValue=True}}"
Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Expander>
Converter:
class CustomVisibilityToBool : MarkupExtension, IValueConverter
{
public bool VisibleValue { get; set; }
public bool CollapsedValue { get; set; }
public bool HiddenValue { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((value as Visibility?) == Visibility.Visible) return VisibleValue;
else if ((value as Visibility?) == Visibility.Hidden) return HiddenValue;
else if ((value as Visibility?) == Visibility.Collapsed) return CollapsedValue;
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((value as bool?) == VisibleValue) return Visibility.Visible;
else if ((value as bool?) == HiddenValue) return Visibility.Hidden;
else if ((value as bool?) == CollapsedValue) return Visibility.Collapsed;
return Binding.DoNothing;
}
}
Which if you place next to the DataGrid
will look like:
add a comment |
You could use something along the lines of:
<Expander Grid.Column="0" ExpandDirection="Right" Width="auto" IsExpanded="false" Header="Options" Background="#ececec" Padding="5">
<StackPanel>
<Label Content="Columns:" FontWeight="SemiBold" />
<ItemsControl ItemsSource="{Binding ElementName=DataGridName, Path=Columns}" Grid.IsSharedSizeScope="True" Margin="5">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A"/>
<ColumnDefinition SharedSizeGroup="B"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Header}" Margin="20, 5, 5, 0"/>
<CheckBox Grid.Column="1" IsChecked="{Binding Visibility, Converter=cnv:CustomVisibilityToBool CollapsedValue=False, VisibleValue=True}}"
Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Expander>
Converter:
class CustomVisibilityToBool : MarkupExtension, IValueConverter
{
public bool VisibleValue { get; set; }
public bool CollapsedValue { get; set; }
public bool HiddenValue { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((value as Visibility?) == Visibility.Visible) return VisibleValue;
else if ((value as Visibility?) == Visibility.Hidden) return HiddenValue;
else if ((value as Visibility?) == Visibility.Collapsed) return CollapsedValue;
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((value as bool?) == VisibleValue) return Visibility.Visible;
else if ((value as bool?) == HiddenValue) return Visibility.Hidden;
else if ((value as bool?) == CollapsedValue) return Visibility.Collapsed;
return Binding.DoNothing;
}
}
Which if you place next to the DataGrid
will look like:
add a comment |
You could use something along the lines of:
<Expander Grid.Column="0" ExpandDirection="Right" Width="auto" IsExpanded="false" Header="Options" Background="#ececec" Padding="5">
<StackPanel>
<Label Content="Columns:" FontWeight="SemiBold" />
<ItemsControl ItemsSource="{Binding ElementName=DataGridName, Path=Columns}" Grid.IsSharedSizeScope="True" Margin="5">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A"/>
<ColumnDefinition SharedSizeGroup="B"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Header}" Margin="20, 5, 5, 0"/>
<CheckBox Grid.Column="1" IsChecked="{Binding Visibility, Converter=cnv:CustomVisibilityToBool CollapsedValue=False, VisibleValue=True}}"
Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Expander>
Converter:
class CustomVisibilityToBool : MarkupExtension, IValueConverter
{
public bool VisibleValue { get; set; }
public bool CollapsedValue { get; set; }
public bool HiddenValue { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((value as Visibility?) == Visibility.Visible) return VisibleValue;
else if ((value as Visibility?) == Visibility.Hidden) return HiddenValue;
else if ((value as Visibility?) == Visibility.Collapsed) return CollapsedValue;
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((value as bool?) == VisibleValue) return Visibility.Visible;
else if ((value as bool?) == HiddenValue) return Visibility.Hidden;
else if ((value as bool?) == CollapsedValue) return Visibility.Collapsed;
return Binding.DoNothing;
}
}
Which if you place next to the DataGrid
will look like:
You could use something along the lines of:
<Expander Grid.Column="0" ExpandDirection="Right" Width="auto" IsExpanded="false" Header="Options" Background="#ececec" Padding="5">
<StackPanel>
<Label Content="Columns:" FontWeight="SemiBold" />
<ItemsControl ItemsSource="{Binding ElementName=DataGridName, Path=Columns}" Grid.IsSharedSizeScope="True" Margin="5">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A"/>
<ColumnDefinition SharedSizeGroup="B"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Header}" Margin="20, 5, 5, 0"/>
<CheckBox Grid.Column="1" IsChecked="{Binding Visibility, Converter=cnv:CustomVisibilityToBool CollapsedValue=False, VisibleValue=True}}"
Margin="5" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Expander>
Converter:
class CustomVisibilityToBool : MarkupExtension, IValueConverter
{
public bool VisibleValue { get; set; }
public bool CollapsedValue { get; set; }
public bool HiddenValue { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((value as Visibility?) == Visibility.Visible) return VisibleValue;
else if ((value as Visibility?) == Visibility.Hidden) return HiddenValue;
else if ((value as Visibility?) == Visibility.Collapsed) return CollapsedValue;
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((value as bool?) == VisibleValue) return Visibility.Visible;
else if ((value as bool?) == HiddenValue) return Visibility.Hidden;
else if ((value as bool?) == CollapsedValue) return Visibility.Collapsed;
return Binding.DoNothing;
}
}
Which if you place next to the DataGrid
will look like:
answered Nov 22 '18 at 10:31
AlfieAlfie
563520
563520
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427710%2fplacing-a-context-menu-on-the-last-data-column-of-data-grid-to-control-the-visib%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Last column should DataGridTemplateColumn->Control->ContextMenu->ItemsControl->Checkboxes->Bind-To-Visibiity.
– Kaspar
Nov 22 '18 at 10:00