Binding visibility to isChecked value of a checkbox - c#

I'm trying to bind my stackpanel's visibility to the checkbox's isChecked value. It's a common problem, but I just can't figure it out.
Converter:
<Page.Resources>
<common:BooleanToVisibilityConverter x:Key="BoolToVis"/>
</Page.Resources>
StackPanel I want to mess with(I removed some code, so don't you worry about listview's binding, I set it in c#):
<ListView x:Name="aktualniGracze" ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="stackLiczymy" Visibility="{Binding isChecked, ElementName=czyLiczymy, Converter={StaticResource BoolToVis}}">
<TextBlock Text="{Binding ileWypil}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
CheckBox:
<CheckBox Name="czyLiczymy"/>
Nothing happens when I change the state of the checkbox, any clues?
Thanks a lot in advance.

If you have your correct DataContext in CheckBox, I think you are just missing the DataContext.isChecked
<ListView x:Name="aktualniGracze" ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="stackLiczymy" Visibility="{Binding DataContext.isChecked, ElementName=czyLiczymy, Converter={StaticResource BoolToVis}}">
<TextBlock Text="{Binding ileWypil}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

Related

How to restrict length of cell in ListView?

I have such a list view
And there is a .xalm
...
<ListView
x:Name="LVLog"
ToolTip="Log of task(s) execution"
Background="WhiteSmoke"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>
...
And there is how I add items in LVLog
LVLog.Items.Add(message.Log);
As you can see if line a long enough it goes out of the borders and user need to scroll horizontaly in order to read log up to the end.
Question is: is there is a way to write line at the next line if it came to the borders?
You could use a TextBlock as an ItemTemplate and set TextTrimming on it. So the text will be trimmed
Long long lo..
and the full text you will see in tooltip:
<ListView ItemsSource="{Binding YourCollection}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ...}" TextTrimming="CharacterEllipsis" ToolTip="{Binding ...}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
If you want to have more lines, then just set TextWrapping="Wrap" in the TextBlock:
<ListView ItemsSource="{Binding YourCollection}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ...}" TextWrapping="Wrap"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

How to bind properties of one ObservableCollection of ListView to properties of SelectedItem of another ListView?

So I have a few ListViews. The first is binded to ObservaleCollection<ComPort>. All properties of ComPort may take some predefined values. Other ListViews are responsible for that properties: they show all that possible (predefined) values and SelectedItem should be the current value of that property of ComPort from the first ObservaleCollection.
I can't attach images so here is an external picture, it would make the situation clean: http://i.stack.imgur.com/ZBRRx.png
<Window.Resources>
<ResourceDictionary x:Name="rd">
<l:ComPorts x:Key="vComPorts"/>
<l:SystemPorts x:Key="vSystemPorts"/>
<l:BaudRates x:Key="vBaudRate"/>
<l:Parities x:Key="vParities"/>
<l:DataBits x:Key="vDataBits"/>
<l:StopBits x:Key="vStopBits"/>
<l:Timeouts x:Key="vTimeouts"/>
<l:ComPort x:Key="vSelectedPort"/>
</ResourceDictionary>
</Window.Resources>
...
<ListView
Name="PortsList"
Grid.Row="1"
Grid.Column="0"
Margin="5"
VerticalAlignment="Stretch"
ItemsSource="{StaticResource vComPorts}"
DataContext="{StaticResource vComPorts}"
SelectedValuePath="PortName"
SelectedValue="{Binding ElementName=SystemPortsList, Path=SelectedItem.Value}"
SelectionChanged="PortsList_SelectionChanged"
MouseDoubleClick="PortsList_MouseDoubleClick">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox />
<TextBlock Margin="5,0,0,0" Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView
x:Name="SystemPortsList"
Margin="5"
VerticalAlignment="Stretch"
DataContext="{Binding Source={StaticResource vSelectedPort}}"
ItemsSource="{Binding Source={StaticResource vSystemPortsView}}"
SelectedItem="{Binding Source={StaticResource vSelectedPort}, Path=PortName}"
MouseEnter="SystemPortsList_Refresh"
MouseLeave="SystemPortsList_Refresh"
Grid.Row="1"
Grid.Column="1" SelectionChanged="SystemPortsList_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Name="tb" Margin="5,0,0,0" Text="{Binding Path=Name}" />
</StackPanel>
</ListView.ItemTemplate>
</ListView>
I've tried to make an instance of class ComPort for saving current value of selected item from the first ListView, but anyway I can't cope with it without help. How this task should be solved?
1) Instead of handling SelectionChanged on the PortsList ListView, bind your checkbox to the ListViewItemsPanel like so:
<CheckBox IsChecked={Binding IsSelected, RelativeSource=Parent/>
2) Add an x:Name to your first ListBox, say x:Name="ComPortLB";
3) Remove DataContext on SystemPortsList;
4) Fix SelectedItem on SystemPortsList like so:
SelectedValue="{Binding ElementName=ComPortLB, Path=SelectedValue.PortName}"
I haven't tested any of this code and I haven't done this kind of stuff for a while, so I apologize for errors, but it should get you closer. I've also had to make some assumptions about your classes since you don't provide enough information.

How to change value of control inside pivot datatemplate on listpicker_selectedIndexChagned event

<phone:Pivot Title="Bank" x:Name="pivotBank"
ItemsSource="{Binding PivotItems}">
<phone:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding StrBankName}"/>
</DataTemplate>
</phone:Pivot.HeaderTemplate>
<phone:Pivot.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel>
<toolkit:ListPicker x:Name="listPickerAccountNumber" SelectionChanged="listPickerAccountNumber_SelectionChanged"
ItemsSource="{Binding listAccountDetails}" >
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding StrAccountNumber}" />
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding StrAccountNumber}" />
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
<TextBlock Text="Account Detail"></TextBlock>
<TextBlock Name="textBlockAccountNumber" Text="{Binding StrAccountNumber}"></TextBlock>
<TextBlock Name="textBlockBalance" Text="{Binding DblBalance}"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
Above pivot control contains headers as bank names and pivot datatemplate contains list of accounts. I want to change the control values of textBlockAccountNumber and textBlockBalance on SelectedIndexChanged event of listPicker.
My question is, is there any way to find control by name from the pivot. I used INotifyPropertyChanged Interface and implemented event with handler but not able to get the rid of it.

WPF ListView ItemTemplate too large space per Label

I have a data bound ListView that uses a rather strange heights for every ListViewItem after I start using an ItemTemplate.
<ListView ItemsSource="{Binding Path=AllTvShows}">
<ListView.ItemTemplate>
<DataTemplate>
<Label FontStretch="Normal" VerticalAlignment="Center" Content="{Binding Path=Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This is how it looks like:
Without the ItemTemplate the labels have the normal height (more adjacent). How should I specify the Label so that it would render normal?
Use a TextBlock instead of a Label:
<ListView ItemsSource="{Binding Path=AllTvShows}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

XAML layout ItemsControl

I have searched extensively on Google and am struggling to find an easy example to follow for an ItemsControl which I think I need to be able to do the following.
I currently have a Grid with a scrollable Listbox containing Checkboxes which works as expected using the following XAML layout
<Grid>
<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding Selections}" Margin="12,22,12,94">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}"
Content="{Binding Path=Item.SelectionName}">
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I now want to add a TextBox to the right of each Checkbox so it's aligned horizontally with 1 Checkbox and 1 Textbox per row. I thought I would have to use an ItemsControl to allow two controls but using the ItemsControl as below I lose the ability to scroll
<Grid>
<ItemsControl ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding Selections}" Margin="12,22,12,94">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}"
Content="{Binding Path=Item.SelectionName}">
</CheckBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Also, if I try and add a textbox similar to
<DataTemplate>
<CheckBox IsChecked="{Binding sChecked}" Content="{Binding Path=Item.BookieName}" />
<TextBox />
</DataTemplate>
I get an error The object 'DataTemplate' already has a child and cannot add 'TextBox'
Essentially, I want it to look like this
Can anyone give me a few pointers on how to structure the controls in XAML to get my desired layout?
Just use a StackPanel in your DataTemplate and set the orientation to horizontal.
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding sChecked}" Content="{Binding Path=Item.BookieName}" />
<TextBox />
</StackPanel>
</DataTemplate>

Categories