2013-01-21 48 views
0

任何人都可以幫助我在自定義Silverlight UserControl中創建ItemsSource屬性嗎?在自定義Silverlight UserControl中創建DependencyPropety

這是我的一個很簡單的視圖模型:

public class MyVM 
{ 
    public ObservableCollection<int> Values { set; get; } 

    public MyVM() 
    { 
     this.Values = new ObservableCollection<int>(); 
    } 
} 

這是我的(內部)用戶控件,我過去到主UserContorl(的MainPage):

<UserControl x:Class="SilverlightApplication1.SilverlightControl1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignHeight="300" d:DesignWidth="400"> 

<Grid x:Name="LayoutRoot" Background="White" > 
    <Border BorderBrush="Black" BorderThickness="2"> 
     <ListBox Margin="5" Name="lst" /> 
    </Border> 
</Grid> 
</UserControl> 

public partial class SilverlightControl1 : UserControl 
{ 
    public IEnumerable MyItemsSource 
    { 
     get 
     { 
      return (IEnumerable)GetValue(MyItemsSourceProperty); 
     } 
     set 
     { 
      SetValue(MyItemsSourceProperty, value); 
     } 
    } 
    public static readonly DependencyProperty MyItemsSourceProperty = 
     DependencyProperty.Register("MyItemsSource", typeof(IEnumerable), typeof(SilverlightControl1), new PropertyMetadata(null)); 


    public SilverlightControl1() 
    { 
     InitializeComponent(); 
    } 
} 

這是一個小容器哪些主機我的用戶控件:

<UserControl x:Class="SilverlightApplication1.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:SilverlightApplication1" 
mc:Ignorable="d" 
d:DesignHeight="300" d:DesignWidth="400"> 

<Grid x:Name="LayoutRoot" Background="White"> 
    <local:SilverlightControl1 Name="qqq" MyItemsSource="{Binding Path=Values}"/> 

</Grid> 
</UserControl> 

public partial class MainPage : UserControl 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 
     MyVM vm = new MyVM(); 
     vm.Values.Add(1); 
     vm.Values.Add(2); 
     vm.Values.Add(3); 
     vm.Values.Add(4); 

     this.DataContext = vm; 
    } 
} 

如何將數據綁定到我的內部ListBox?

回答

0

我已經完全使用用戶控件停止的部分原因是因爲這個。我使用CustomControls代替。可用性和抱怨是完全一樣的。您可能有代碼隱藏和單獨的模型(儘管我主要使用控件的代碼作爲模型本身設置this.DataContext = this;)。我的代碼也變得更加結構化(至少我是這麼認爲的),並且我有更多的可能性來改變控件的UI。

使用CustomControls的唯一缺點是我沒有像在UserControls和Windows中那樣的設計表面。用戶界面放置在Generic.xaml文件中(默認情況下),並通過在編輯器中編寫XAML來構建,而不是使用鼠標。首先,這是一個無賴,但我已經習慣了它驚人的快。

所以我的答案是,如果你使用CustomControls,你可以做到完全一樣。該綁定是這樣寫的:

MyItemsSource="{Binding RelativeSource="{RelativeSource Mode=TemplatedParent}" Path=Values}" 

...或者乾脆:

MyItemsSource="{TemplateBinding Values}" 

...如果你沒有添加別的綁定任何轉換器或任何東西。

0

首先,您的SilverlightControl1沒有自己的DataContext,這意味着它繼承了其容器的DataContext(在這種情況下爲MainPage)。我會回答假設你希望以這種方式設置(與SilverlightControl1具有自己的DC相反)。這就是說,MyItemsSource是無用的。你可以把它放在一起。從MainPage.xaml刪除它,只是包括控制像這樣:

<Grid x:Name="LayoutRoot" Background="White"> 
    <local:SilverlightControl1 x:Name="qqq" /> 
</Grid> 

其次,你的列表框未綁定到任何東西。既然是繼承主要的DC,可以綁定到Values像這樣:

<Grid x:Name="LayoutRoot" Background="White" > 
    <Border BorderBrush="Black" BorderThickness="2"> 
     <ListBox Margin="5" Name="lst" ItemsSource="{Binding Values}" /> 
    </Border> 
</Grid> 
相關問題