I持ってWindow
(削除不要なセクション)に以下の内容:WPF:次のコードをProcedural(C#)からDeclarative(XAML)に変換できますか?
XAML:
<Style x:Key="itemstyle" TargetType="{x:Type ContentPresenter}">
<EventSetter Event="MouseLeftButtonDown" Handler="HandleItemClick"/>
</Style>
<ItemsControl ItemsSource="{Binding ArtistList}" Margin="10" Name="artist_list" ItemContainerStyle="{StaticResource itemstyle}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ID}" Foreground="White"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Controls:RSSViewer x:Name="rssControl" />
のC#(の後ろコード):今すぐ
private void HandleItemClick(object sender, MouseButtonEventArgs e)
{
var selectedArtist = ((ContentPresenter) sender).Content as Artist;
rssControl.SourceUrl = "http://agnt666laptop:28666/rss.aspx?artistid=" + selectedArtist.ID;
}
私がしたいのは、xamlの上記の混合をa WPFのDataBindingモデルを利用するために、C#を純粋にxamlだけにする必要があります。
itemscontrol要素の選択された項目などのイベントトリガーやデータバインディングの組み合わせなどが必要だと思うが、どうやってそれをどうやって行くのか分からない。
手順コードを削除するために上記のソリューションをどのように変換することができますか?
System.Windows.Dataエラー:4: 'ElementName = artist_list'という参照でバインド元が見つかりません。 BindingExpression:Path = SelectedItem.ID; DataItem = null;ターゲット要素は 'RSSViewer'(Name = 'rssControl')です。ターゲットプロパティが 'SourceUrl'(型 '文字列')です。 –
ItemsControlにSelectedItemsプロパティがないと思います。選択が必要な場合は、ListBoxまたはListViewを使用する必要があります。 – Andy
Andyさん、ItemsControlにはSelectedItemプロパティがありません。別の方法で処理する必要があります。 –