2013-01-16 133 views
0

我有一個:在一個非常特殊的情況下WPF與ContentPresenters「無法綁定找到源」,和用戶控件

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=Test'. BindingExpression:Path=Value; DataItem=null; target element is 'Slider' (Name=''); target property is 'Value' (type 'Double')

錯誤。 我雖然關於名稱範圍的問題,但我不知道如何解決它。

考慮以下WPF應用程序:

MyUserControl.xaml

<UserControl x:Class="WpfApplication1.MyUserControl" 
      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" mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 

    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 

     <Label Grid.Row="0" Content="Move me !"/> 
     <Slider x:Name="Test" Grid.Row="0" Grid.Column="1" /> 
     <Label Grid.Row="1" Content="Binded slider"/> 
     <ContentPresenter Grid.Row="1" Grid.Column="1"> 
      <ContentPresenter.Content> 
       <Slider Value="{Binding Value, ElementName=Test}"/> 
      </ContentPresenter.Content> 
     </ContentPresenter> 
    </Grid> 
</UserControl> 

MyUserControl.xaml.cs

using System.Windows.Controls; 

namespace WpfApplication1 
{ 
    public partial class MyUserControl : UserControl 
    { 
     public MyUserControl() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="300" Height="120"> 

    <StackPanel> 
     <Button Click="Button_Click" Content="Click me to show Sliders !" Height="25"/> 
     <ContentPresenter x:Name="contentPresenter"/> 
    </StackPanel> 
</Window> 

MainWindow.xaml.cs

using System.Windows; 

namespace WpfApplication1 
{ 
    public partial class MainWindow : Window 
    { 
     private MyUserControl myUserControl; 

     public MainWindow() 
     { 
      InitializeComponent(); 

      myUserControl = new MyUserControl(); 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      contentPresenter.Content = myUserControl; 
     } 
    } 
} 

鑑於MyUserControl.xaml的代碼,我希望綁定的滑塊具有相同的值具有第一位的。

但沒有任何反應。

現在,棘手的部分:啓動應用程序,單擊按鈕,移動第一個滑塊,打開「WPF Inspector」並將其附加到應用程序。結果:綁定是固定的。

你如何解釋這種現象?

+0

我測試了這一點,它什麼毛病在從代碼添加後的MyUserControl的結合...內容。如果您從工具箱中將它添加到網格,則綁定將起作用。 – Mihai

回答

0

這將這樣的伎倆

<!--<ContentPresenter Grid.Row="1" Grid.Column="1"> 
     <ContentPresenter.Content>--> 
      <Slider Value="{Binding Value, ElementName=Test}" Grid.Column="1" Grid.Row="1"/> 
     <!--</ContentPresenter.Content> 
    </ContentPresenter>--> 

因爲滑塊是一個內容展示器裏面,你不能訪問它就像該值。您需要綁定到相同的viewmodel屬性才能在內容展示器中擁有它。

編輯

Get Prism和用戶視圖模型......我就不贅述了......但是,你需要做的是什麼...

你donwloaded後一切加Microsoft.Practices.Prism dll到您的項目

創建一個新的類(ViewModel).Name它任何你想要的。我打電話給我的MyUserControlViewModel,使它看起來像這樣

using Microsoft.Practices.Prism.ViewModel; 

namespace YourNamespace 
{ 
    public class MyUserControlViewModel : NotificationObject 
    { 
     private double _sliderValue; 

     public double SliderValue 
     { 
      get { return _sliderValue; } 
      set 
      { 
       _sliderValue = value; 
       RaisePropertyChanged(() => SliderValue); 
      } 
     } 

    } 
} 

更改XAML在你的用戶控件這樣

<Slider x:Name="Test" Grid.Row="0" Grid.Column="1" Value="{Binding SliderValue, Mode=TwoWay}"/>   
<ContentPresenter Grid.Row="1" Grid.Column="1"> 
    <ContentPresenter.Content> 
     <Slider Value="{Binding SliderValue}"/> 
    </ContentPresenter.Content> 
</ContentPresenter> 

並添加此行代碼放到你的MyUserControl。CS文件

DataContext = new MyUserControlViewModel(); 

現在視圖模型接管重任......你綁定的第一個滑塊的值將物業SliderValue(該Mode=TwoWay表明,這種控制可以設置和獲取屬性的狀態)並且您已經綁定的其他滑塊一樣SliderValue使其同步移動與第一

希望這有助於

+0

我必須將ContentPresenter和Slider放在裏面。 那麼,當你說:「你需要綁定到相同的viewmodel屬性才能在內容演示者中擁有該屬性。」 ? –

+0

感謝您的詳細信息。有用。 但我仍然想知道爲什麼當我把 myUserControl = new MyUserControl();在Button_Click方法中,它的工作原理是 。 –