オブジェクトの1つにイベントハンドラーを必要とするDataTemplateがあります。このDataTemplateはResourceDictionaryに含まれています。このテンプレートにイベントハンドラーを追加する最良の方法は何ですか?
app.xaml.csでイベントハンドラーを定義しようとしましたが、ハンドラーが実行されていません。ResourceDictionaryのコードビハインドファイルを作成すると、MergedDictionariesでアプリを起動するときにロードエラーが発生します。
GraphStyles.xamlから
<DataTemplate x:Key="PieTemplate">
<Grid HorizontalAlignment="Left" Width="350" Height="350" >
<Border>
<Charting:Chart
x:Name="PieChart"
Title="Play Attempts"
Margin="70,0" Loaded="PieChart_Loaded">
<Charting:Chart.Series>
<Charting:PieSeries
Title="Attempts"
ItemsSource="{Binding Items}"
IndependentValueBinding="{Binding Name}"
DependentValueBinding="{Binding Value}"
IsSelectionEnabled="True" />
</Charting:Chart.Series>
</Charting:Chart>
</Border>
</Grid>
</DataTemplate>
App.Xaml.csで
private void PieChart_Loaded(object sender, RoutedEventArgs e)
{
var pieChart = sender as Chart;
var legendItems = ((PieSeries)pieChart.Series[0]).LegendItems;
foreach (LegendItem item in legendItems)
{
pieChart.LegendItems.Add(item);
pieChart.LegendStyle = item.Style;
}
}