Chart в ControlTemplate
От: Аноним  
Дата: 30.06.11 07:31
Оценка:
Добрый день!
Столкнулся с такой проблемой.
Есть UserControl в котором находилась круговая диаграмма. Все работало хорошо, пока не потребовалось менять GUI для этого UserControl.
Я выделил все это в ControlTemplate и диаграмма перестала показываться. ToolTip над ней показывается но самой нет. И при изменении значений в ItemsSource вылетает исключение KeyNotFoundException.

   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at System.Windows.Controls.DataVisualization.Charting.PieSeries.OnDataPointIndependentValueChanged(DataPoint dataPoint, Object oldValue, Object newValue)
   at System.Windows.Controls.DataVisualization.Charting.DataPointSeries.OnDataPointIndependentValueChanged(Object sender, RoutedPropertyChangedEventArgs`1 args)
   at System.Windows.Controls.DataVisualization.Charting.DataPoint.OnIndependentValuePropertyChanged(Object oldValue, Object newValue)
   at System.Windows.Controls.DataVisualization.Charting.DataPoint.OnIndependentValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp)
   at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.ScheduleTransfer(Boolean isASubPropertyChange)
   at MS.Internal.Data.ClrBindingWorker.NewValueAvailable(Boolean dependencySourcesChanged, Boolean initialValue, Boolean isASubPropertyChange)
   at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange)
   at MS.Internal.Data.ClrBindingWorker.OnSourcePropertyChanged(Object o, String propName)
   at MS.Internal.Data.PropertyPathWorker.System.Windows.IWeakEventListener.ReceiveWeakEvent(Type managerType, Object sender, EventArgs e)
   at System.Windows.WeakEventManager.DeliverEventToList(Object sender, EventArgs args, ListenerList list)
   at System.ComponentModel.PropertyChangedEventManager.OnPropertyChanged(Object sender, PropertyChangedEventArgs args)


Вот диаграмма.
 <Charting:Chart BorderThickness="0" Style="{DynamicResource TimeChartStyle}"         
                 BorderBrush="Transparent"
                 Width="160" Height="160" Background="Transparent"
                 HorizontalAlignment="Center" VerticalAlignment="Center" 
                 Palette="{DynamicResource TimePiePalette}">
                        <Charting:Chart.LegendStyle>
                            <Style TargetType="Control">
                                <!-- setting Visibility to Collapsed does not work -->
                                <Setter Property="Template" Value="{x:Null}"/>
                            </Style>
                        </Charting:Chart.LegendStyle>
                        <Charting:Chart.PlotAreaStyle>
                            <Style TargetType="Grid">
                                <Setter Property="Background" Value="Transparent"/>
                            </Style>
                        </Charting:Chart.PlotAreaStyle>            
                        <Charting:Chart.Series>
                            <Charting:PieSeries Background="Transparent"                                                 
                                    BorderThickness="0" BorderBrush="{DynamicResource ChildControlBackground}"
                                    TransitionDuration="0"
                                    Grid.Row="0" Grid.Column="2"                                     
                                    ItemsSource="{Binding TimeCollection}"
                                    DependentValueBinding="{Binding Lines}"
                                    IndependentValueBinding="{Binding DisplayName}"/>
                        </Charting:Chart.Series>
                    </Charting:Chart>


Стиль диаграммы
    <Style x:Key="TimeChartStyle" TargetType="Charting:Chart">
        <Setter Property="PlotAreaStyle">
            <Setter.Value>
                <Style TargetType="Grid">
                    <Setter Property="Background" Value="{DynamicResource ChildControlBackground}" />
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Charting:Chart">
                    <Border Background="{TemplateBinding Background}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    Padding="10">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>                        

                            <Grid Margin="0,15,0,15" Grid.Row="1">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>

                                <chartingprimitives:EdgePanel x:Name="ChartArea"
                                               Style="{TemplateBinding ChartAreaStyle}">
                                    <Grid Style="{TemplateBinding PlotAreaStyle}" Canvas.ZIndex="-1" />
                                 
                                </chartingprimitives:EdgePanel>
                            </Grid>

                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>



    <ControlTemplate x:Key="TimePieDataPointTemplate" TargetType="Charting:PieDataPoint">
        <Grid x:Name="Root" Opacity="0">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualStateGroup.Transitions>
                        <VisualTransition GeneratedDuration="0:0:0.1"/>
                    </VisualStateGroup.Transitions>
                    <VisualState x:Name="Normal"/>
                    <VisualState x:Name="MouseOver">
                        <Storyboard>
                            <DoubleAnimation
                                        Storyboard.TargetName="MouseOverHighlight"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0.6" Duration="0"/>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
                <VisualStateGroup x:Name="SelectionStates">
                    <VisualStateGroup.Transitions>
                        <VisualTransition GeneratedDuration="0:0:0.1"/>
                    </VisualStateGroup.Transitions>
                    <VisualState x:Name="Unselected"/>
                    <VisualState x:Name="Selected">
                        <Storyboard>
                            <DoubleAnimation
                                        Storyboard.TargetName="SelectionHighlight"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0.6" Duration="0"/>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
                <VisualStateGroup x:Name="RevealStates">
                    <VisualStateGroup.Transitions>
                        <VisualTransition GeneratedDuration="0:0:0.5"/>
                    </VisualStateGroup.Transitions>
                    <VisualState x:Name="Shown">
                        <Storyboard>
                            <DoubleAnimation
                                        Storyboard.TargetName="Root"
                                        Storyboard.TargetProperty="Opacity"
                                        To="1" Duration="0"/>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Hidden">
                        <Storyboard>
                            <DoubleAnimation
                                        Storyboard.TargetName="Root"
                                        Storyboard.TargetProperty="Opacity"
                                        To="0" Duration="0"/>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <Path
                        x:Name="Slice"
                        Data="{TemplateBinding Geometry}"
                        Fill="{TemplateBinding Background}"
                        Stroke="{TemplateBinding BorderBrush}"
                        StrokeMiterLimit="1">
                <ToolTipService.ToolTip>
                    <StackPanel>
                        <ContentControl Content="{Binding DisplayName}"/>
                    </StackPanel>
                </ToolTipService.ToolTip>
            </Path>
            <Path x:Name="SelectionHighlight"
                        Data="{TemplateBinding GeometrySelection}"
                        Fill="Red"
                        StrokeMiterLimit="1"
                        IsHitTestVisible="False"
                        Opacity="0"/>
            <Path
                        x:Name="MouseOverHighlight"
                        Data="{TemplateBinding GeometryHighlight}"
                        Fill="Green"
                        StrokeMiterLimit="1"
                        IsHitTestVisible="False"
                        Opacity="0"/>
        </Grid>
    </ControlTemplate>

    <visualizationToolkit:ResourceDictionaryCollection x:Key="TimePiePalette">
        <!-- Gray-->
        <ResourceDictionary>
            <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
                <GradientStop Color="LightGray"/>
                <GradientStop Color="Gray" Offset="1"/>
            </RadialGradientBrush>
            <Style x:Key="DataPointStyle" TargetType="Control">
                <Setter Property="Template" Value="{StaticResource TimePieDataPointTemplate}"/>
                <Setter Property="Background" Value="{StaticResource Background}"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
            </Style>
        </ResourceDictionary>

        <!-- Green-->
        <ResourceDictionary>
            <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" 
                                         Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9">
                <GradientStop Color="LightGreen"/>
                <GradientStop Color="Green" Offset="1"/>
            </RadialGradientBrush>
            <Style x:Key="DataPointStyle" TargetType="Control">
                <Setter Property="Template" Value="{StaticResource TimePieDataPointTemplate}"/>
                <Setter Property="Background" Value="{StaticResource Background}"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
            </Style>
        </ResourceDictionary>
    </visualizationToolkit:ResourceDictionaryCollection>
Re: Chart в ControlTemplate
От: motormanyak Земля  
Дата: 30.06.11 10:45
Оценка:
Здравствуйте, Аноним, Вы писали:

А>Вот диаграмма.

А>
А> ...
А>                                    ItemsSource="{Binding TimeCollection}"
А>                                    DependentValueBinding="{Binding Lines}"
А>                                    IndependentValueBinding="{Binding DisplayName}"/>
А>



Почему-то мне кажется, что дело именно в этих 3-х строках. нельзя ли посмотреть на TimeCollection?
In God We Trust
Re[2]: Chart в ControlTemplate
От: Аноним  
Дата: 30.06.11 14:54
Оценка:
Здравствуйте, motormanyak, Вы писали:

M>Здравствуйте, Аноним, Вы писали:


А>>Вот диаграмма.

А>>
А>> ...
А>>                                    ItemsSource="{Binding TimeCollection}"
А>>                                    DependentValueBinding="{Binding Lines}"
А>>                                    IndependentValueBinding="{Binding DisplayName}"/>
А>>



M>Почему-то мне кажется, что дело именно в этих 3-х строках. нельзя ли посмотреть на TimeCollection?

Не думаю, что в этом дело. Раньше этот код работал без проблем.
Вот

private readonly ObservableCollection<TimeElement> _timeCollection = new ObservableCollection<TimeElement>();                                       
        public ObservableCollection<TimeElement> TimeCollection
        {
            get { return _timeCollection; }            
        }



 public class TimeElement: INotifyPropertyChanged
    {
        private string _displayName;
        public string DisplayName
        {
            get { return _displayName; }
            set
            {
                _displayName = value;
                var handler = PropertyChanged;
                if (null != handler)
                    handler.Invoke(this, new PropertyChangedEventArgs("DisplayName"));
            }
        }
      
        public int Lines
        {
            get { return _lines; }
            set
            {
                _lines = value;
                var handler = PropertyChanged;
                if (null != handler)
                    handler.Invoke(this, new PropertyChangedEventArgs("Lines"));
            }
        }
        private int _lines;

        public event PropertyChangedEventHandler PropertyChanged;    
    }
Re[3]: Chart в ControlTemplate
От: motormanyak Земля  
Дата: 30.06.11 16:03
Оценка:
Здравствуйте, Аноним, Вы писали:

А>Здравствуйте, motormanyak, Вы писали:


M>>Здравствуйте, Аноним, Вы писали:


а если так


<Window x:Class="KillWpf.Window1" Name="mainWnd"
.....
 ItemsSource="{Binding TimeCollection, ElementName=mainWnd}"
In God We Trust
Re[4]: Chart в ControlTemplate
От: Аноним  
Дата: 30.06.11 18:57
Оценка:
Здравствуйте, motormanyak, Вы писали:

M>Здравствуйте, Аноним, Вы писали:


А>>Здравствуйте, motormanyak, Вы писали:


M>>>Здравствуйте, Аноним, Вы писали:


M>а если так



M>
M><Window x:Class="KillWpf.Window1" Name="mainWnd"
M>.....
M> ItemsSource="{Binding TimeCollection, ElementName=mainWnd}"
M>


Спасибо, но судя по всему проблема не в этом.
Сейчас запустил проект на другом компе — все работает нормально.
Буду смотреть чего не так на моем)))
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.