にキャストします。私のXAMLには2つのタブがあり、最初にParameterFilesListというObservableCollectionオブジェクトにバインドされたListBoxと、CalcFilesListというObservableCollectionオブジェクトにバインドされたListBoxがあります。ListBoxアイテムをコレクションタイプ
<UserControl x:Class="Sym.VisualStudioExtension.Engines.TAEngineView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:behaviours="clr-namespace:Sym.VisualStudioExtension"
xmlns:local="clr-namespace:Sym.VisualStudioExtension"
local:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="700" d:DesignWidth="400">
<Grid>
<TabControl x:Name="tabControl" HorizontalAlignment="Left" Height="490" Margin="19,44,-36,-234" VerticalAlignment="Top" Width="317">
<TabItem Header="Parameter Files">
<ListBox Margin="20" ItemsSource="{Binding ParameterFilesList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</TabItem>
<TabItem Header="Calc Files">
<ListBox Margin="20" ItemsSource="{Binding CalcFilesList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</TabItem>
</TabControl>
<Label x:Name="label" Content="{Binding Path=Title}" HorizontalAlignment="Left" Margin="19,13,0,0" VerticalAlignment="Top" Width="367
" BorderThickness="2"/>
</Grid>
アイテムをダブルクリックすると、リスト内の名前が表示されているファイルが開きます。まず、リスト項目をParameter/CalcFileオブジェクトに戻す必要があります。
現在、私はダブルクリックしてコードビハインドで処理します。そして、私の考えはのViewModelのOpenOnDoubleClickメソッドのパラメータまたはCalcFileのタイプを決定することであったが、私はどのように知りません
private void ListBox_MouseDoubleClick(object sender, RoutedEventArgs e)
{
dynamic viewModel = DataContext;
viewModel.OpenOnDoubleClick(sender, e);
}
をそれをやる。私はキャストエラーを取得します。
送信者はListBoxItemですが、ViewModelのParameter/CalcFileにキャストするにはどうすればよいですか?
私は、実際にParameter/CalcFileをxaml.csファイルに持っていきたいとは思っていません。私はそれをモデルとは別にしておき、ダブルクリックイベントを処理してビューモデルに渡すために仲介者として使用する方が好きです。
ああ、と私はParameterListBox_MouseDoubleClick
とCalcFileListBox_MouseDoubleClick
を持っているし、その後のViewModelに2つの異なる方法ように私は、別のタブに異なるイベントハンドラを持っているので、パラメータリスト用とCalcFileリストに1つのことができると思います。