Enable/Disable Button When Listbox is Selected - c#

I have a ListBox
<ListBox Background="WhiteSmoke"
Name="LstComponents"
ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ComponentID}" />
<TextBlock Text=" - " />
<TextBlock Text="{Binding ComponentName}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and a few buttons
<Button Content="Add New Components"
Name="BtnAdd"
Margin="5"
Click="BtnAdd_Click" />
<Button Content="Update Components"
Name="BtnUpdate"
Grid.Column="1"
Margin="5"
Click="BtnUpdate_Click" />
<Button Content="Delete Components"
Grid.Column="2"
Name="BtnDelete"
Margin="5"
Click="BtnDelete_Click" />
Now I wanted the Update and Delete button to be disabled when it is loaded whereas it should enable if the list item in the ListBox is selected.
I don't want to write a code behind for it. Plz Suggest? How would i be able to accomplish this task in XAML

You can use
IsEnabled="{Binding ElementName=ListBoxName, Path=SelectedItems.Count}"
to make it work.

Related

TreeView subcollection autosort when properties changes

I would like to sort the second level of my TreeView. The sorting depends on two different conditions depending on the FieldType and the Order properties. In my case it is possible that the order increases or decreases by the user. The user uses arrow up and down buttons. How can I keep the list sortet if an item is added or removed and the user changes the Order property?
My current TreeView:
<TreeView Grid.Row="0" x:Name="tvImages" ItemsSource="{Binding SelectedTemplate.Images}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type entities:TemplateImageDTO}" ItemsSource="{Binding FieldSections}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Order, StringFormat=Image {0}}" Margin="0,3,0,0"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type entities:FieldSectionDTO}">
<DockPanel LastChildFill="False" HorizontalAlignment="Stretch" Width="Auto">
<StackPanel DockPanel.Dock="Left" Orientation="Horizontal">
<TextBlock Text="{Binding FieldType, Converter={StaticResource enumConverter}}" Margin="0,3,0,0" />
<TextBlock Text=" - Field " Margin="0,3,0,0" />
<TextBlock Text="{Binding Order, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,3,0,0" />
</StackPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Right" Margin="0,0,10,0">
<Button Width="24" Height="24" Style="{DynamicResource MahApps.Metro.Styles.MetroCircleButtonStyle}">
<iconPacks:PackIconFontAwesome Kind="AngleUpSolid" Foreground="{DynamicResource AccentColorBrush}" />
</Button>
<Button Width="24" Height="24" Margin="10,0,0,0" Style="{DynamicResource MahApps.Metro.Styles.MetroCircleButtonStyle}">
<iconPacks:PackIconFontAwesome Kind="AngleDownSolid" Foreground="{DynamicResource AccentColorBrush}" />
</Button>
</StackPanel>
</DockPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
I found my Solution here: https://stackoverflow.com/a/19363888/6751312
After the user clicked the arrow up and down button, I increase and decrease the order property and resort the list.

TextBox does not exist in the current context in DataGrgid DataRowTemplate

I have a datagrid which on click will show a textbox for user input. I am able to display the details on the textbox. Now I have a Update button inside the datatemplate. Once i click on the update button I want the user input taken and do processing.
This is my xaml code:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Border BorderThickness="0" Background="BlanchedAlmond" Padding="10">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="12" Text="First Name: " VerticalAlignment="Center" />
<TextBox x:Name="txtFirstName" FontSize="16" Foreground="MidnightBlue" Text="{Binding UserFirstName}" VerticalAlignment="Center" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="12" Text="Last Name: " VerticalAlignment="Center" />
<TextBox x:Name="txtLastName" FontSize="16" Foreground="MidnightBlue" Text="{Binding UserLastName}" VerticalAlignment="Center" />
</StackPanel>
<StackPanel>
<Button x:Name="btnUpdate" Content="Update" VerticalAlignment="Center" HorizontalAlignment="Right" Click="btnUpdate_Click"/>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
This is my .cs code:
void btnUpdate_Click(object sender, RoutedEventArgs e)
{
string firstName;
firstName = txtFirstName.Text;
}
The txtFirstName.Text is showing Does Not Exist In The Current Context
Because it's inside the datatemplate you may have to do something like https://code.msdn.microsoft.com/windowsapps/How-to-access-a-control-6039571a
Or use the binding that you already have, just make the mode TwoWay.
{Binding UserFirstName, Mode=TwoWay}

WPF ListView deselect item

I have a single Grid that contains a ListView. The ListView is populated with 2 records of employees. How can I detect if focus is lost. E.g the empty space below records or another part of the window is clicked. The record should then be deselected.
<Grid HorizontalAlignment="Left" Height="128" Margin="10,39,0,0" VerticalAlignment="Top" Width="267" LostFocus="Grid_LostFocus">
<ListView Margin="0" Name="listviewEmps" MouseDoubleClick="listviewEmps_MouseDoubleClick" SelectionChanged="listviewEmps_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="Name: " />
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBlock Text=", " />
<TextBlock Text="dob: " />
<TextBlock Text="{Binding DOB , StringFormat={}{0:d}}" />
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>

Event Handler on DataTemplate in App.xaml Windows Phone 8.1

I have a ListView populated by items having a different style thanks to an ItemTemplateSelector. The DataTemplates are placed in app.xaml as ressources like the following:
<DataTemplate x:Key="FollowingOuterTemplate">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="Delete" />
<MenuFlyoutItem Text="Refresh" />
<MenuFlyoutItem Text="Share" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
<StackPanel Orientation="Vertical" Width="282">
<TextBlock Grid.Row="0" FontSize="33" Text="{Binding Pseudo}" Foreground="Gray" Height="46" Margin="0,0,-0.333,0"/>
<TextBlock Grid.Row="1" FontSize="20" Text="{Binding NomPrenom}" Foreground="#5bc5eb" Height="27" Margin="0,0,-0.333,0"/>
<TextBlock Grid.Row="2" FontSize="20" Text="Appuyez pour ne plus suivre" Foreground="#BCC6CC" Height="27" Margin="0,0,-0.333,0"/>
</StackPanel>
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" Width="113">
<Image Name="StatutContact" Height="43" Source="/Ressources/Images/checkedTests2.png" Stretch="Fill" Margin="0,20,0,0" Width="44" HorizontalAlignment="Center"/>
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
So now I would like to show the FlyoutBase attached menu when I'm holding an Item. But as the DataTemplate is in app.xaml and it's forbidden to add event handler there (app.xaml.cs is not having any constructor as it's not constructing pages). I would like to add a Holding event handler to the StackPanel that the FyloutBase Menu is attached to.
Anyone knows how to achieve that ?
Not sure if you really just cannot do that, but Shawn Kendrot proposed a port of the Silverlight Toolkit's ContextMenuService that gets around your problem on his Blog.

Have a clickable button on top of a combobox in WPF

I can get a button to appear and be clickable in the drop down list of a combo box, but I cannot get the selected combo box item (the drop list is closed) to have the button be clickable. It always skips the button click and just opens the drop down list. I basically want the Button_Click event handler that I setup to be called once it is clicked. Here is my sample combo box that shows the button but is not clickable once it is in the selected item:
<ComboBox x:Name="MyCombo" Width="200" Height="30" ItemsSource="{Binding ListCombo}">
<ComboBox.Resources>
<DataTemplate DataType="{x:Type local:ComboItemClass}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=SampleText}" Width="120" />
<Button Width="20" Content="..." Click="Button_Click"/>
</StackPanel>
</DataTemplate>
</ComboBox.Resources>
</ComboBox>
Putting buttons inside comboboxes is one of those really cool features that we now CAN do in WPF that we (me included) get really excited about, before we stop to consider if we SHOULD do this.
Having a button inside a combobox makes it very easy to confuse the heck out of your user. I would recommend that you bind the data from your comobox listitems to a button outside of the combobox, where your user will expect it. That way, you can still change the end result of the button being pressed by selecting an item from the combobox.
EDIT:
If you have the space for it, a listbox would work perfectly for what you want to do.
<ListBox>
<ListBoxItem>
<StackPanel Height="34" HorizontalAlignment="Left" Margin="12,16,0,0" VerticalAlignment="Top" Width="430" Orientation="Horizontal">
<Button Content="Edit" />
<Button Content="Delete" />
<TextBlock Text="Port Information here" VerticalAlignment="Center" Margin="20,0" />
</StackPanel>
</ListBoxItem>
<ListBoxItem>
<StackPanel Height="34" HorizontalAlignment="Left" Margin="12,16,0,0" VerticalAlignment="Top" Width="430" Orientation="Horizontal">
<Button Content="Edit" />
<Button Content="Delete" />
<TextBlock Text="Port Information here" VerticalAlignment="Center" Margin="20,0" />
</StackPanel>
</ListBoxItem>
<ListBoxItem>
<StackPanel Height="34" HorizontalAlignment="Left" Margin="12,16,0,0" VerticalAlignment="Top" Width="430" Orientation="Horizontal">
<Button Content="Edit" />
<Button Content="Delete" />
<TextBlock Text="Port Information here" VerticalAlignment="Center" Margin="20,0" />
</StackPanel>
</ListBoxItem>

Categories

Resources