How to set up a binding from DataGridColumns to a multi-selection ComboBox? - wpfdatagrid

I have a DataGrid and a custom ComboBox. The ComboBox used a ListBox to enable multi-select.
I intend to give a ObservableCollection<Data> Datas property as the data source for the ComboBox,where Data is a class with string name and bool checked. The problem is how to set up each column's visibility to a corresponding CheckBoxwhich used a Dataas its source ?
<Style x:Key="CheckBoxListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Foreground" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid x:Name="RootElement">
<CheckBox ClickMode="Press" Content="{Binding Path=Name}" IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" Foreground="White"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I saw some post used something like the xmal below, but obviously I do not has any ElementName here.
<DataGridTextColumn Header="Price" Binding="{Binding Price}" IsReadOnly="False"
Visibility="{Binding IsChecked,
Converter={StaticResource visibilityConverter},
ElementName=chkShowPrice}"/>

This binding is not straight forward as it looks. Since DataGridTextColumn does not fall in the same visual tree as DataGrid, so it is not possible to bind the Visibility of the column directly. However, a bit tricky approach but you may use x:Reference for the same.
Here is how we can do it
XAML
<!--I added this element to provide the DataContext by using x:Reference-->
<FrameworkElement x:Name="contextProvider" />
<DataGrid Grid.Column="1"
AutoGenerateColumns="False"
ItemsSource="{Binding DD}"
x:Name="dg"
Style="{StaticResource DataGridDemoStyle}">
<DataGrid.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</DataGrid.Resources>
<i:Interaction.Behaviors>
<behavior:DataGridDropBehavior></behavior:DataGridDropBehavior>
</i:Interaction.Behaviors>
<DataGrid.Columns>
<DataGridTextColumn x:Name="Column3"
Binding="{Binding Symbol}"
Header="Symbol"
Width="*"
IsReadOnly="True"
Visibility="{Binding DataContext.Data[0].IsSelected, Source={x:Reference contextProvider}, Converter={StaticResource BooleanToVisibilityConverter}}" />
<DataGridTextColumn x:Name="Column4"
Binding="{Binding Ask}"
Header="Ask"
Width="*"
IsReadOnly="True"
Visibility="{Binding DataContext.Data[1].IsSelected, Source={x:Reference contextProvider}, Converter={StaticResource BooleanToVisibilityConverter}}" />
<DataGridTextColumn x:Name="Column5"
Binding="{Binding Bid}"
Header="Bid"
Width="*"
IsReadOnly="True"
Visibility="{Binding DataContext.Data[2].IsSelected, Source={x:Reference contextProvider}, Converter={StaticResource BooleanToVisibilityConverter}}" />
<DataGridTextColumn x:Name="Column6"
Binding="{Binding Volume}"
Header="Vol"
Width="*"
IsReadOnly="True"
Visibility="{Binding DataContext.Data[3].IsSelected, Source={x:Reference contextProvider}, Converter={StaticResource BooleanToVisibilityConverter}}" />
</DataGrid.Columns>
</DataGrid>
I have binded the items from the same Data bound to multi select combo box to the visibility of the columns
changed the following so that the DataContext is available to all the elements instead of just datagrid.
dg.DataContext = d;
to
DataContext = d;
lastly to enable the IsChecked binding modify the CheckBox from
<CheckBox ClickMode="Press" Content="{Binding Path=Name}" IsChecked="true" Foreground="White"/>
to
<CheckBox ClickMode="Press" Content="{Binding Path=Name}" IsChecked="{Binding IsSelected}" Foreground="White"/>
that's all you need for such binding.
here is the link to the provided sample project DataGridColumnsVisibilityBinding.zip
Let me know if this helps.

Related

Tooltip of a List of checkboxes in silverlight

I want to display a list of checkboxes within a tooltip on Silverlight when the mouse hovers on an image.
The data is a list of string properties.
The problem: The checkbox text content disappears, only the checked boxes appear.
How to show both the box and its text content? Thanks.
Tooltip display
<ToolTipService.ToolTip >
<ToolTip d:DataContext="{d:DesignInstance Type=local:Data}">
<ListBox x:Name="LstTemp">
<!--<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalAlignment" Value="{Binding Input}"/>
</Style>
</ListBox.ItemContainerStyle>-->
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="true" IsEnabled="False" Content="{Binding Input}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ToolTip>
</ToolTipService.ToolTip>
</Image>
</Grid>
The code behind
Public Class Data
Public Property Data1 As String
Public Property Data2 As String
Public Property Input As New List(Of String)
End Class
I found the solution, here is the code for a dynamic list of checkboxes within a tooltip
<Image Height="114" HorizontalAlignment="Left" Margin="129,48,0,0" Name="Image1" Stretch="Fill" VerticalAlignment="Top" Width="170" Source="/Tooltip1;component/Images/Desert.jpg" >
<ToolTipService.ToolTip >
<ToolTip d:DataContext="{d:DesignInstance Type=local:Data}">
<ListBox x:Name="LstTemp">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Margin="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<CheckBox IsChecked="true" IsEnabled="false"/>
<ContentPresenter
Grid.Column="1"
Margin="2,0,0,0" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</ToolTip>
</ToolTipService.ToolTip>
</Image>

Windows Store App - XAML - Why is the Textblock is bigger the StackPanel?

I have the below XAML and it does not do what I expect it to do.
<StackPanel Background="{Binding EwsColour}" Visibility="{Binding EwsVisibility}" HorizontalAlignment="Center" VerticalAlignment="Center" >
<TextBlock Text="EWS" Style="{StaticResource SubheaderTextBlockStyle}" VerticalAlignment="Top" FontWeight="SemiBold" HorizontalAlignment="Center" />
<TextBlock Text="18" Style="{StaticResource SubheaderTextBlockStyle}" FontWeight="SemiBold" FontSize="100" />
</StackPanel>
I end up with this
I don't understand why the TextBlock that has 18 in, is growing bigger then then StackPanel. Can anyone tell me why?
After seeing Chris W''s comment I checked the Style in generic.xaml.
<Style x:Key="SubheaderTextBlockStyle" TargetType="TextBlock" BasedOn="{StaticResource BaseTextBlockStyle}">
<Setter Property="FontSize" Value="26.667"/>
<Setter Property="FontWeight" Value="Light"/>
<Setter Property="LineHeight" Value="30"/>
</Style>
I had no idea that there was a LineHeight property. Overriding it so that its the same as the font size (or bigger actually creates a better UX) stops this from happening.
<StackPanel Background="{Binding EwsColour}" Visibility="{Binding EwsVisibility}" HorizontalAlignment="Center" VerticalAlignment="Center" >
<TextBlock Text="EWS" Style="{StaticResource SubheaderTextBlockStyle}" VerticalAlignment="Top" FontWeight="SemiBold" HorizontalAlignment="Center" />
<TextBlock Text="18" Style="{StaticResource SubheaderTextBlockStyle}" FontWeight="SemiBold" FontSize="100" LineHeight="110" />
</StackPanel>

Why does this Parent.Parent.IsChecked binding not work?

I am trying to bind the visibility of a Rectangle with the IsChecked property of the parent's parent which is a ToggleButton. I used the "Create Data Binding..." window to create the binding:
<ToggleButton Margin="20,20,20,0">
<Grid>
<Rectangle Fill="{StaticResource BlueLight}" Visibility="{Binding Parent.Parent.IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource Mode=Self}}" />
<TextBlock Margin="5,0,5,5" Text="Values" />
</Grid>
</ToggleButton>
The binding works in the designer, but when I run the program it does not.
If I change the binding to the following it works, but I would rather not specify a name for each of the ToggleButton objects I create.
<ToggleButton x:Name="valueToggleButton" Margin="20,20,20,0">
<Grid>
<Rectangle Fill="{StaticResource BlueLight}" Visibility="{Binding IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=valueToggleButton}" />
<TextBlock Margin="5,0,5,5" Text="Values" />
</Grid>
</ToggleButton>
What am I doing wrong to get the relative binding to work? Or is this a WinRT issue/limitation?
Bind to Ancestor like below:
<ToggleButton Margin="20,20,20,0">
<Grid>
<Rectangle Fill="{StaticResource BlueLight}" Visibility="{Binding IsChecked, Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource FindAncestor,AncestorType=ToggleButton}}" />
<TextBlock Margin="5,0,5,5" Text="Values" />
</Grid>
</ToggleButton>

How to bind the items count of listview in XAML

I have a problem with binding the count to the textblock.
I have a listview that it's items are binded to a Users object properties.
I want to get the count of all items that were binded.
Here is my ListView:
<ListView ItemsSource="{Binding Users}" x:Name="lvUsers">
<ListView.ItemTemplate>
<DataTemplate>
<ListViewItem IsHitTestVisible="False">
<StackPanel>
<facebookControls:ProfilePicture Height="74" Width="74" ProfileId="{Binding FacebookId}" />
<TextBlock Text="{Binding UserName}" FontSize="18" HorizontalAlignment="Center" />
</StackPanel>
</ListViewItem>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And here is my TextBlock:
<TextBlock Text="{Binding ElementName=lvUsers, Path=Items.Count, Mode=OneWay}" />
What am I doing wrong?
I know that if my ListView will be without Templates, just ListViewItems, it will work, but I need it to be like that.
Override the template for this ListView.
The sample below basically creates a style where it overrides the control template such that the regular ListView items (the ItemsPresenter) is wrapped in the StackPanel so that the TextBlock with count or whatever else can be added at the bottom.
<ListView ItemsSource="{Binding Users}">
<ListView.Style>
<Style TargetType="{x:Type ListView}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border BorderThickness="1" BorderBrush="LightGray">
<StackPanel>
<ScrollViewer>
<ItemsPresenter />
</ScrollViewer>
<TextBlock Margin="0,4" FontWeight="Bold">
<Run Text="Count: "/>
<Run Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListView}}, Path=Items.Count, Mode=OneWay}"/>
</TextBlock>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</ListView.Style>
<ListView.ItemTemplate>
<DataTemplate>
<ListViewItem IsHitTestVisible="False">
<StackPanel>
<facebookControls:ProfilePicture Height="74" Width="74" ProfileId="{Binding FacebookId}" />
<TextBlock Text="{Binding UserName}" FontSize="18" HorizontalAlignment="Center" />
</StackPanel>
</ListViewItem>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

How do I achieve a 2 -way data bind with an applied style in xaml?

I've got this bit of code in my page.xaml
<TextBox x:Name="NameTextField" Grid.ColumnSpan="7" Grid.Column="1" Text="{Binding Name, Mode=TwoWay}" Style="{StaticResource TextBoxStyle}" />
It refers to this style:
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid x:Name="grid" Height="55" Background="White">
<Rectangle Stroke="#FFD9D9D9" StrokeThickness="6"/>
<ContentPresenter x:Name="contentPresenterText" VerticalAlignment="Center" Margin="6,0" Height="42" >
<TextBox Text="{TemplateBinding Text}" FontSize="21.333" FontFamily="Arial" FontWeight="Bold"/>
</ContentPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This works ok when pre-populating data from the bind but does not seem to work the other way, when data is entered.
Is there something obvious I'm missing here?
Many thanks
Try changing:
<TextBox Text="{TemplateBinding Text}" FontSize="21.333" FontFamily="Arial" FontWeight="Bold"/>
to:
<TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, Path=Text}" FontSize="21.333" FontFamily="Arial" FontWeight="Bold"/>
TemplateBinding seems to default to a one-way bind.