How to change Highlight color of the selected ListView item in UWP (Windows 10)

Actually a better way to discover the styling properties is to use Blend. First, open up your page in Blend. Then right click on your ListView and go Edit Additional Templates > Edit Generated Item Container (ItemContainerStyle) > Edit a Copy. Give it a name and hit OK. Now, you have generated the full built-in … Read more

How can I tell my DataTemplate to bind to a property in the PARENT ViewModel?

The answer is this: <DataTemplate x:Key=”CodeGenerationMenuTemplate”> <MenuItem Header=”{Binding Title}” Command=”{Binding DataContext.SwitchPageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}” CommandParameter=”{Binding Title}”/> </DataTemplate> I just saw that Nir had given me the syntax to solve the above issue on this question: What is the best way in MVVM to build a menu that displays various pages?.

Sort a wpf datagrid programmatically

voo’s solution was not working for me, ItemsSource was null, most likely because it was not directly set, but bound. All other solutions I found here at StackOverflow were dealing with sorting the Model only, but the DataGrid header was not reflecting to the sort. Here’s a proper solution based on the incomplete script here: … Read more

Setting the Visibility of an Element to Collapsed when Storyboard completes using XAML

you can do this in the animation as well <Window.Resources> <Storyboard x:Key=”OnLoaded1″> <ObjectAnimationUsingKeyFrames BeginTime=”00:00:00″ Storyboard.TargetName=”button” Storyboard.TargetProperty=”(UIElement.Visibility)”> <DiscreteObjectKeyFrame KeyTime=”00:00:00″ Value=”{x:Static Visibility.Visible}”/> <DiscreteObjectKeyFrame KeyTime=”00:00:00.8000000″ Value=”{x:Static Visibility.Collapsed}”/> <DiscreteObjectKeyFrame KeyTime=”00:00:01.4000000″ Value=”{x:Static Visibility.Visible}”/> </ObjectAnimationUsingKeyFrames> </Storyboard> </Window.Resources> <Window.Triggers> <EventTrigger RoutedEvent=”FrameworkElement.Loaded”> <BeginStoryboard Storyboard=”{StaticResource OnLoaded1}”/> </EventTrigger> </Window.Triggers>

How do you pass parameters from xaml?

Your constructor: public ClockControl(String city) { InitializeComponent(); this.initController(); //… } First of all, if you want to use ClockControl from XAML, then you need a default constructor, means a constructor which doesn’t take any parameter. So the above constructor is not going to work. I would suggest you to define a property with name City, … Read more

How to set Control Template in code?

Creating template in codebehind is not a good idea, in theory one would do this by defining the ControlTemplate.VisualTree which is a FrameworkElementFactory. ControlTemplate template = new ControlTemplate(typeof(Button)); var image = new FrameworkElementFactory(typeof(Image)); template.VisualTree = image; Assigning properties is very roundabout since you need to use SetValue and SetBinding: image.SetValue(Image.SourceProperty, …); Also, about the (previously) … Read more

How can I sort a ListBox using only XAML and no code-behind?

Use a CollectionViewSource: <CollectionViewSource x:Key=”SortedItems” Source=”{Binding CollectionOfStrings}” xmlns:scm=”clr-namespace:System.ComponentModel;assembly=Win‌​dowsBase”> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName=”SomePropertyOnYourItems”/> </CollectionViewSource.SortDescriptions> </CollectionViewSource> <ListBox ItemsSource=”{Binding Source={StaticResource SortedItems}}”/> You might want to wrap your strings in a custom VM class so you can more easily apply sorting behavior.

Mutually exclusive checkable menu items?

This may not be what you’re looking for, but you could write an extension for the MenuItem class that allows you to use something like the GroupName property of the RadioButton class. I slightly modified this handy example for similarly extending ToggleButton controls and reworked it a little for your situation and came up with … Read more

Remove Highlight Effect from ListViewItem

I don’t know if this is the only solution but I did it as follows (by setting the Template property of the ListViewItems): <ListView.ItemContainerStyle> <Style TargetType=”{x:Type ListViewItem}”> <Setter Property=”Background” Value=”Transparent” /> <Setter Property=”Template”> <Setter.Value> <ControlTemplate TargetType=”{x:Type ListViewItem}”> <Border BorderBrush=”Transparent” BorderThickness=”0″ Background=”{TemplateBinding Background}”> <GridViewRowPresenter HorizontalAlignment=”Stretch” VerticalAlignment=”{TemplateBinding VerticalContentAlignment}” Width=”Auto” Margin=”0″ Content=”{TemplateBinding Content}”/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> … Read more

Access codebehind variable in XAML

There are a few ways to do this. Add your variable as a resource from codebehind: myWindow.Resources.Add(“myResourceKey”, myVariable); Then you can access it from XAML: <TextBlock Text=”{StaticResource myResourceKey}”/> If you have to add it after the XAML gets parsed, you can use a DynamicResource above instead of StaticResource. Make the variable a property of something … Read more

tech