DependencyProperty.RegisterAttached和多个实例

时间:2012-10-02 13:34:29

标签: wpf mvvm dependency-properties

我正在研究WPF MVVM应用程序。我正在寻找将WebBrowser控件数据绑定到视图模型,而视图模型又绑定到Tab。按照这个article中的建议,我创建了一个由静态DependancyProperty组成的静态助手类:

public static class WebBrowserHelper
{
    public static readonly DependencyProperty BodyProperty =
        DependencyProperty.RegisterAttached("Body", typeof(string), typeof(WebBrowserHelper), new PropertyMetadata(OnBodyChanged));

    public static string GetBody(DependencyObject dependencyObject)
    {
        return (string)dependencyObject.GetValue(BodyProperty);
    }

    public static void SetBody(DependencyObject dependencyObject, string body)
    {
        dependencyObject.SetValue(BodyProperty, body);
    }

    private static void OnBodyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        string newValue = (string)e.NewValue;
        var webBrowser = (WebBrowser)d;
        webBrowser.NavigateToString(newValue);
    }
}

XAML将WebBrowser绑定到DependancyProperty:

<WebBrowser Grid.Column="2" HorizontalAlignment="Center" src:WebBrowserHelper.Body="{Binding HTMLBody}"  VerticalAlignment="Center" Height="Auto" Width="Auto"  />

绑定到Tab Control的ItemsSource的ViewModel:

public class SomeVM : ViewModelBase, INotifyPropertyChanged
{

    private string _htmlBody;
    private SomeView _myView = new SomeView();

    public SomeVM (string tabName)
    {
        TabName = tabName;
        string contentsAsHTML = do_a_whole_bunch_of_stuff_to_generate_an_HTML_string();
        HTMLBody = contentsAsHTML;
    }

    public string HTMLBody
    {
        get { return _htmlBody; }
        set
        {
            if (_htmlBody != value)
            {
                _htmlBody = value;
                RaisePropertyChanged("HTMLBody");
            }
        }
    }

    public SomeView View
    {
        get {return _myView;}
        set { }
    }

    public string TabName { get; set; }
}

MainViewModel,创建Tab集合:

    private ObservableCollection<SomeVM> _tabs;
    public ObservableCollection<SomeVM> Tabs
    {
        get
        {
            if (_tabs== null)
            {
                _tabs= new ObservableCollection<SomeVM>();
                _tabs.Add(new SomeVM("Tab 1"));
                _tabs.Add(new SomeVM("Tab 2"));
                _tabs.Add(new SomeVM("Tab 3"));
            }
            return _tabs;
        }
    }

MainWindow.xaml设置Tab Binding:

           <TabControl ItemsSource="{Binding Tabs, Source={StaticResource vm}}"
                        >
                <TabControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock
                            Text="{Binding TabName}" />
                    </DataTemplate>
                </TabControl.ItemTemplate>
                <TabControl.ContentTemplate>
                    <DataTemplate>
                        <ContentPresenter Content="{Binding View}" />
                    </DataTemplate>
                </TabControl.ContentTemplate>
           </TabControl>

我的问题是“OnBodyChanged”会在标签更改时多次触发。 HTML需要几秒钟才能加载,我宁愿它只在视图模型中实际修改属性时加载。

修改

Here's the smallest sample project that recreates my problem

1 个答案:

答案 0 :(得分:1)

您的问题与附加属性或MVVM无关。 实际上,真正的问题是TabControl每次更改选定的选项卡时都会销毁并重新创建其子项。这可以解释为什么处理程序被多次调用。 VisualTree仅包含选定的选项卡。

如果您可以尝试使用其他控件,则会看到没有错误。

要解决此问题,我会将您重定向到此post