WPF Button Height same as ListItem height - wpf

I have a ListBox, In it I have a custom DataTemplate set for the ListBox.ItemTemplate so the ListBox Items are Radio buttons styled as rounded buttons.
Is there any way I could bind another separate button on my View to the Height of the ListBox.ItemTemplate's RadioButton's ActualHeight, so the separate button's height would always be the same height as the Radiobuttons in the ListBox?

Here's a solution with some code behind. Handle the SizeChanged event for the RadioButton, and assign the given height to the view model's Height property. Bind the separate button's Height property to the Height property in the view model. Here's the XAML:
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<RadioButton
Content="{Binding Name}"
SizeChanged="OnRadioButtonSizeChanged" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button
Content="Button"
Height="{Binding Height}"
/>
And here's the code behind:
private void OnRadioButtonSizeChanged(object sender, SizeChangedEventArgs e)
{
ViewModel.Height = e.NewSize.Height;
}
Of course, the view model's Height property should raise the PropertyChanged event.

Related

ListBox and Checkbox - XAML/C# - Windows 8 App

I have a little problem with my Project. I have an ListBox with a Checkbox for each item. The Checkbox takes the Content from ListBox, but how do I connect the Selection of the Item and the IsChecked from the Checkbox?
My plan is that when you Check the Checkbox that the line with the Checked Checkbox will be deleted. How do I do that?
My XAML so far:
<ListBox x:Name="To_do_Liste" ItemsSource="{Binding}" Height="593"
Margin="0,85,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5"
BorderBrush="#FDFFE818" Background="#FFFFE818"
IsSynchronizedWithCurrentItem="False" IsDoubleTapEnabled="True"
IsHoldingEnabled="True" IsRightTapEnabled="True"
ScrollViewer.VerticalScrollBarVisibility="Auto" ManipulationMode="All"
FontFamily="SketchFlow Print" HorizontalAlignment="Left" Width="320"
SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<ListBoxItem HorizontalContentAlignment="Stretch">
<CheckBox x:Name="CheckBox1" Foreground="Black"
BorderBrush="#FF007FFF" Content="{Binding}" FontFamily="SketchFlow Print"
FontSize="26" />
</ListBoxItem>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
you can add selectionchange eventhandler to listbox and here you can check which checkbox is selected either using selectedindex of some other property of checkbox and now update your OBSERVABLECOLLECTION to with you listbox is binded.
private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
CheckBox chkbx = sender as CheckBox;
}

Binding Text Property of child element of ComboBoxItem

I have a WPF ComboBox with a static list of people with an image and first name. I am trying to bind the selected FirstName to a db table. The way I am doing it now (obviously wrong) I am only binding the object and hence writing:
System.Windows.Controls.ComboBoxItem
to my db table.
How can I bind the FirstName from the selected ComboBoxItem from the TextBlock.Text property? Is there a way to do this purely in WPF?
<ComboBox Text="{Binding Path=FirstName}">
<ComboBoxItem>
<StackPanel Orientation="Horizontal" >
<Image Source="/Images/Alice.png" />
<TextBlock Text="Alice" />
</StackPanel>
</ComboBoxItem>
<ComboBoxItem>
<StackPanel Orientation="Horizontal">
<Image Source="/Images/Bob.png" />
<TextBlock Text="Bob" />
</StackPanel>
</ComboBoxItem>
</ComboBox>
I am saving the changes to the db table on a button click event
private void SaveAndClose_Click(object sender, RoutedEventArgs e)
{
bindingView = (BindingListCollectionView)myCollection.View;
bindingView.CommitEdit();
db.SubmitChanges();
}
Instead of hard-coding the comboboxitems, you should use a datatemplate. Then, you should use the SelectedValuePath to select the value based on the FirstName property in your items. Then you can bind to the textbox.
If you want to bind to the textbox in xaml directly, you'll need to name the element (x:Name="myTextBox"). Then, bind your combobox's SelectedValue like so: SelectedValue="{Binding ElementName="myTextBox", Path="Text"}".
In general, I don't think binding a combobox to a textbox is a very good idea.

How can I set the XAML for the first WPF RadioButton in a ListView control to be checked by default?

I have a WPF ListView control containing a number of RadioButton controls using data binding. I'd like the first RadioButton in the group to be checked by default, preferably set in the XAML rather than programmatically, but haven't managed to achieve this.
My XAML for the control is:
<ListView ItemsSource="{Binding OptionsSortedByKey}" >
<ListView.ItemTemplate>
<DataTemplate DataType="{x:Type Logging:FilterOptionsRadioListViewModel}">
<RadioButton Content="{Binding Value}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The OptionsSortedByKey property is a SortedList.
I have done this clumsily by setting the IsChecked property of the RadioButton control in the Loaded event:
var button = sender as RadioButton;
if (button != null)
{
if (button.Content.ToString().ToUpperInvariant() == "ALL")
{
button.IsChecked = true;
}
}
I'd far prefer to do it via data binding in the XAML. Is there a simple way?
You could:
<RadioButton IsChecked="{Binding RelativeSource={PreviousData}, Converter={StaticResource NullAsTrueConverter}}"/>
Where the converter returns true if the value is null.
That said, an MVVM-based approach would make this a snap. You'd just:
<RadioButton IsChecked="{Binding IsChecked}"/>
And then your primary view model would set IsChecked to true for the first child view model in the collection.

Command Binding in UserControl used as ListBox.ItemTemplate

I have a Listbox with a UserControl as the DataTemplate. This UserControl has a Button to remove that item from the list.
<ListBox x:Name="FileList" ItemsSource="{Binding Files}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Views:FileItem/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The ItemsSource is defined as:
ObservableCollection<FileViewModel> m_fileViews = new ObservableCollection<FileViewModel>();
Here is the UserControl simplified:
<UserControl x:Class="Views.FileItem">
<Canvas x:Name="LayoutRoot">
<TextBlock x:Name="FileName" Text="{Binding FileName}" />
<Button Content="Remove"/>
</Canvas>
</UserControl>
When the user clicks the Remove button, it should remove this item from the ObservableCollection.
The problem is, the DataContext for each ListBoxItem is a different ViewModel than the ViewModel that holds the ObservableCollection.
I'm not sure how to bind the Remove button to an ICommand in the "parent" ViewModel. Any help would be appreciated. Thanks so much.
I would bind the button to an ICommand in the UserControl's ViewModel, then send a message across to the parent ViewModel using loosely-coupled messaging (available in most Mvvm frameworks, like MvvmFoundation)
Let the parent VM register for the 'remove me' message, and update the ObservableCollection accordingly...
Hope this helps :)

Silverlight: How to dynamically bind a ComboBox in a ListBox ItemTemplate?

I have a list box that requires at least one ComboBox. I couldn't find a way to place the ComboBox in the ItemTemplate I use.
...
<DataTemplate x:Key="parts_template">
<StackPanel Orientation="Horizontal">
<TextBlock .../>
<ComboBox .../>
</StackPanel>
</DataTemplate>
...
<ListBox x:Name="lb_parts" ItemTemplate="{StaticResource parts_template}" .../>
...
How do bind that ComoBox in the DataTemplate to an ObservableCollection in the code behind?
Another thing you could try is subscribe the Loaded event on the ComboBox.
Then you can set the ComboBox.ItemsSource in the EventHandler to MyObservableCollection.
Have a look
XAML:
<DataTemplate x:Key="parts_template">
<StackPanel Orientation="Horizontal">
<TextBlock .../>
<ComboBox Loaded="ComboBox_OnLoaded">
<!-- ComboBox ItemTemplate -->
</ComboBox>
</StackPanel>
</DataTemplate>
C# Code Behind:
private void ComboBox_OnLoaded(object sender, EventArgs e)
{
((ComboBox)sender).ItemsSource = MyObservableCollection;
}
Okay, here is how you can add a ComboBox to the ListBox in the code behind.
Create a ComboBox
ComboBox x = new ComboBox();
If you have a data source that populates the ComboBox then you can just bind that
x.ItemsSource = e.Result;
If you do not and want to manually add items to the ComboBox:
ComboBoxItem y = new ComboBoxItem();
Set the Content of the Item to what you want displayed in the ComboBox
y.Content = "Hello";
Now all that is left, is to add the ComboBoxItem to the ComboBox (only if you are creating the Items manually), and then the ComboBox to the ListBox
x.Items.Add(y);
//testing is my ListBox
testing.Items.Add(x);
You should be able to set the data context to the List itself
lb_Parts.DataContext=myCollection;
Then you should be able to bind to it in the template
<DataTemplate x:Key="parts_template">
<StackPanel Orientation="Horizontal">
<TextBlock .../>
<ComboBox ItemSource={Binding}/>
</StackPanel>
</DataTemplate>

Resources