Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all.

I have a WPF custom control placed to grid. There is some standard wpf controls placed to same grid. My custom control has a binded dependency property. Parent grid have a datatrigger on Property Visibility and triggered when DataContext is not null.

Using my custom control:

XML
<Grid Margin="5" DataContext="{Binding DetailedView}" >
    <Grid.Resources>
        <Style TargetType="{x:Type Grid}">
            <Setter Property="Visibility" Value="Visible"/>
                <Style.Triggers>
                     <DataTrigger Binding="{Binding}" Value="{x:Null}">
                          <Setter Property="Visibility" Value="Collapsed"/>
                     </DataTrigger>
                </Style.Triggers>
            </Style>
    </Grid.Resources>
    <!- ... -->
    <!-- Grid layout -->
    <!-- ... -->
    <Grid Grid.Column="1" Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="320"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="32"/>
            <RowDefinition Height="32"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <!-- My Custom control -->
        <controls:RatingControl x:Name="RcMangaRating" Grid.Column="1" Grid.Row="0" Value="{Binding Rating}" Size="24" Maximum="10" HorizontalAlignment="Right"/>
        <!-- Standard WPF control which have same binding as custom control -->
        <TextBlock Grid.Column="1" Grid.Row="1" FontSize="12" VerticalAlignment="Top" Text="{Binding Rating, StringFormat='{}Rating: {0:F2} from 10'}" Margin="0,5,5,0" HorizontalAlignment="Right"/>
    </Grid>
</Grid>


So, when visibility changed from Collapsed to Visible the binding for custom control not working but for standard control binding works normally. http://i.stack.imgur.com/bzZc7.png[^]

But when grid visibility set to Visible and not changing the binding for custom control works normally. http://i.stack.imgur.com/ny5EH.png[^]

Region of custom control style:

XML
<Rectangle Grid.Row="0" Height="{TemplateBinding Width}" Width="{TemplateBinding Height}">
    <Rectangle.Style>
        <Style>
             <Setter Property="Rectangle.OpacityMask">
                <Setter.Value>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                        <GradientStop Color="Black" Offset="{Binding Value, RelativeSource={RelativeSource TemplatedParent}}"/>
                        <GradientStop Color="Transparent" Offset="{Binding Value, RelativeSource={RelativeSource TemplatedParent}}"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>
    </Rectangle.Style>
</Rectangle>


What I have tried:

I delete trigger on Visibility property and all bindings works normally

XML
<Grid Margin="5" DataContext="{Binding DetailedView}" >
    <!-- ... -->
    <!-- Grid layout -->
    <!-- ... -->
    <Grid Grid.Column="1" Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="320"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="32"/>
            <RowDefinition Height="32"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <!-- My Custom control -->
        <controls:RatingControl x:Name="RcMangaRating" Grid.Column="1" Grid.Row="0" Value="{Binding Rating}" Size="24" Maximum="10" HorizontalAlignment="Right"/>
        <!-- Standard WPF control which have same binding as custom control -->
        <TextBlock Grid.Column="1" Grid.Row="1" FontSize="12" VerticalAlignment="Top" Text="{Binding Rating, StringFormat='{}Rating: {0:F2} from 10'}" Margin="0,5,5,0" HorizontalAlignment="Right"/>
    </Grid>
</Grid>


Have you any ideas what is problem?

--
Thanks you in advance.

Problem solved

Solution:

So, at first, there is the problem was in OnApplyTemplate in my control. I get TemplatePart in this method and initialize some fields in control:

C#
public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    _starItems = (ItemsControl)GetTemplateChild("PART_StarItems");
    if (_starItems == null) return;

    var values = Enumerable.Range(0, (int)Maximum).Select(r => 0.0).ToList();
    _starValues = new ObservableCollection<double>(values);
}


Then in callback OnChangeValue applyes _starValues to _starItems. But at first time OnApplyTemplate after initializing is not calling (it calls after OnChangeValue and etc.) and _starItems was null:

C#
RatingControl control = d as RatingControl;
if (control == null) return;
if (control._starItems == null) return; // Here was problem

/* Update values in _starValues */
_starItems.ItemsSource = _starValues;


So, I created new private DependecyProperty Values (same as _starValues) and bind it to ItemsSource of my control and removed TemplatePart and needless fields.

Source code[^]

Control template style[^]

Sorry, the problem was not in control template (how i originally thought), and there is implementation of binding for ItemsSource in my code. By this reason i originally post only XAML code. Thank you.

Thanks to my leader for the help and advice
Posted
Updated 13-Sep-16 2:18am
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900