2009-05-16 19 views
1

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要素の選択された項目などのイベントトリガーやデータバインディングの組み合わせなどが必要だと思うが、どうやってそれをどうやって行くのか分からない。

手順コードを削除するために上記のソリューションをどのように変換することができますか?

答えて

1

.NET 3.5SP1を使用している場合、おそらく新しいStringFormatバインディングマークアップ拡張を使用してそれを行うことができます。 StringFormatによるバインドの例については、hereを参照してください。

.NET 3.5SP1がオプションでない場合は、おそらく独自のValueConverterを作成する必要があります。 SourceUrlプロパティの値を選択したアーティストのIDにバインドし、コンバーターで上記のC#サンプルで使用しているのと同じ文字列を返します。

0
<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" SourceUrl="{Binding SelectedItem.ID, ElementName=artist_list, StringFormat= 'http://agnt666laptop:28666/rss.aspx?artistid={0}' }" /> 
+0

System.Windows.Dataエラー:4: 'ElementName = artist_list'という参照でバインド元が見つかりません。 BindingExpression:Path = SelectedItem.ID; DataItem = null;ターゲット要素は 'RSSViewer'(Name = 'rssControl')です。ターゲットプロパティが 'SourceUrl'(型 '文字列')です。 –

+0

ItemsControlにSelectedItemsプロパティがないと思います。選択が必要な場合は、ListBoxまたはListViewを使用する必要があります。 – Andy

+0

Andyさん、ItemsControlにはSelectedItemプロパティがありません。別の方法で処理する必要があります。 –

関連する問題