2

ボタンにさまざまなウェブサイトのロゴが付いたリストボックスがあり、上部に があるアプリを作成していますWebBrowser。ロゴを押すと、webBrowser が対応するページをロードするという考え方です。私はすでにこれを機能させていますが、MVVM を使用してアプリを作り直して改善したいと考えています。WebBrowserすべてのロゴでリストボックスを作成しましたが、ロゴをクリックしたときにURL をロードする方法がわかりません。

4

2 に答える 2

7

これが Phone7 で機能するかどうかは 100% 確信が持てませんが、試してみる価値はあります...

最初のWebBrowserSource プロパティは ではないためバインド可能ではないため、バインドを支援するDependancyPropertyを作成するヘルパー クラスを作成する必要があります。AttachedProperty

次に、ListBoxItem からの実際のリンクを含むプロパティを使用してListBox SelectedItem、新しいプロパティにリンクできます。LinkSource

例:

Xaml:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication8"
        Title="MainWindow" Height="233" Width="405" Name="UI">

    <StackPanel Orientation="Horizontal" DataContext="{Binding ElementName=UI}">
        <ListBox x:Name="listbox" ItemsSource="{Binding Links}" Width="100" DisplayMemberPath="Name"/>
        <WebBrowser local:WebBrowserHelper.LinkSource="{Binding ElementName=listbox, Path=SelectedItem.Site}" Width="200"/>
    </StackPanel>

</Window>

コード:

public partial class MainWindow : Window
{
    private ObservableCollection<Link> _links = new ObservableCollection<Link>();

    public MainWindow()
    {
        InitializeComponent();
        Links.Add(new Link { Name = "StackOverflow", Site = new Uri("http://stackoverflow.com/") });
        Links.Add(new Link { Name = "Google", Site = new Uri("http://www.google.com/") });
    }

    public ObservableCollection<Link> Links
    {
        get { return _links; }
        set { _links = value; }
    }
}

// ListBox item
public class Link
{
    public string Name { get; set; }
    public Uri Site { get; set; }
}


// helper calss to create AttachedProperty
public static class WebBrowserHelper
{
    public static readonly DependencyProperty LinkSourceProperty =
        DependencyProperty.RegisterAttached("LinkSource", typeof(string), typeof(WebBrowserHelper), new UIPropertyMetadata(null, LinkSourcePropertyChanged));

    public static string GetLinkSource(DependencyObject obj)
    {
        return (string)obj.GetValue(LinkSourceProperty);
    }

    public static void SetLinkSource(DependencyObject obj, string value)
    {
        obj.SetValue(LinkSourceProperty, value);
    }

    // When link changed navigate to site.
    public static void LinkSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        var browser = o as WebBrowser;
        if (browser != null)
        {
            string uri = e.NewValue as string;
            browser.Source = uri != null ? new Uri(uri) : null;
        }
    }

}

結果:

ここに画像の説明を入力

于 2013-01-28T00:57:08.020 に答える
-1

あなたの質問は実際には2つの質問です。

  1. リストボックスからクリックを拾う方法はいくつかあります。最も基本的なものは<Listbox SelectedItem="{Binding selectedItem,mode=TwoWay}" ...

  2. Web ブラウザーの URL を設定するには、VM に INotifyPropertyChanged を実装public Uri browserUri { get; private set; }し、WM で宣言し、プロパティを変更するときに必ず PropertyChanged を発生させ、XAML で<WebBrowser Source="{Binding browserUri}" />

于 2013-01-28T00:19:55.490 に答える