Search code examples
xamluwpuwp-xamlwinui

UWP xaml property set at UserControl level not taking effect


I have following xaml

<GridView 
    x:Name="gird_view" 
    Grid.Column="1" 
    ItemsSource="{x:Bind workers}"
    ContainerContentChanging="gird_view_ContainerContentChanging">
    <GridView.ItemContainerStyle>
        <Style TargetType="GridViewItem">
            <Setter Property="Margin" Value="10"/>
        </Style>
    </GridView.ItemContainerStyle>
    <GridView.ItemTemplate>
        <DataTemplate x:DataType="models:Worker">
            <local:WorkerControl ViewModel="{x:Bind}"/>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

In another xaml file:

<UserControl
    x:Class="XBind.View.WorkerControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:XBind.View"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400"
    Background="BlueViolet">

    <StackPanel Orientation="Vertical" Width="100" BorderBrush="SkyBlue" BorderThickness="1">
        <TextBlock Text="{x:Bind ViewModel.MyName_First,Mode=OneWay}"/>
        <TextBlock Text="{x:Bind ViewModel.MyAge_Years,Mode=OneWay}"/>
        <TextBlock Text="{x:Bind ViewModel.MyWork_Years,Mode=OneWay}"/>
    </StackPanel>
</UserControl>

Code behind file:

public sealed partial class WorkerControl : UserControl, System.ComponentModel.INotifyPropertyChanged
{
    public WorkerControl()
    {
        this.InitializeComponent();
    }

    ...
}

Background color of each GridViewItem is not coming up as "BlueViolet". If I set the background on StackPanel, it takes effect. Why does it not work when I set it on UserControl? Reason why I am trying this is so that I can set IsEnabled property on UserControl which is not available on StackPanel. Can anyone help?


Solution

  • According to the UWP UserControl documentation, you need to define the background color of the root element.

    Note that UserControl does not manifest the value of its property in its control template. Instead, set the Background of the root element inside the UserControl. For more info, see Remarks in Control.Background.