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:
<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 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>
<controls:RatingControl x:Name="RcMangaRating" Grid.Column="1" Grid.Row="0" Value="{Binding Rating}" Size="24" Maximum="10" HorizontalAlignment="Right"/>
<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:
<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
<Grid Margin="5" DataContext="{Binding DetailedView}" >
<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>
<controls:RatingControl x:Name="RcMangaRating" Grid.Column="1" Grid.Row="0" Value="{Binding Rating}" Size="24" Maximum="10" HorizontalAlignment="Right"/>
<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:
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:
RatingControl control = d as RatingControl;
if (control == null) return;
if (control._starItems == null) return;
_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