2017-06-16 18 views
0

所以我用他的相對視圖模型構建了一個用戶控件。當在ComboBox內單擊一個按鈕時,必須顯示此控件。爲了讓大家更好的想法,我會後我的代碼:用戶控件的Dynamic PlacementTarget?

<ItemsControl Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" HorizontalAlignment="Center" 
      ItemsSource="{Binding MyItems}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Grid HorizontalAlignment="Center" Margin="7,0,10,0"> 
        <Grid.Resources> 
         <CollectionViewSource x:Key="cvs" Source="{Binding CBSource}" /> 
        </Grid.Resources> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="180"/> 
         <ColumnDefinition Width="180"/> 
         <ColumnDefinition Width="180"/> 
        </Grid.ColumnDefinitions> 
        <Label HorizontalContentAlignment="Center" Grid.Column="0" Content="{Binding FirstProperty}"/> 
        <Label HorizontalContentAlignment="Center" Grid.Column="1" Content="{Binding SecondProperty}"/> 
        <Label HorizontalContentAlignment="Center" Grid.Column="2" Content="{Binding ThirdProperty}"/> 
        <ComboBox HorizontalAlignment="Center" Grid.Column="2" Width="140" Visibility="{Binding HasCombobox, Converter={StaticResource BoolToVis}}"> 
         <ComboBox.ItemsSource> 
          <CompositeCollection> 
           <CollectionContainer Collection="{Binding Source={StaticResource cvs}}" /> 
           <ComboBoxItem> 
            <Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Content="{x:Static prop:Resources.INSERT_BTN}" Command ="This will call CustomUserControl"></Button> 
           </ComboBoxItem> 
          </CompositeCollection> 
         </ComboBox.ItemsSource> 
        </ComboBox> 
       </Grid> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

這是我ItemsControl的模板。無論何時驗證屬性,此控件都會顯示ComboBox。所以在我看來,我不知道我會有多少ComboBox以及這些將被放置在哪裏(所有這些ItemsControl的組合創建一種網格)。我的目標是通過一個可變放置目標(它必須在調用它的ComboBox附近彈出)顯示一個小視圖。

要求:我需要一種方法將我的CustomUserControl.xaml放置在我上面描述的ItemControl內,並具有動態放置目標。將調用此控制按鈕,將有一個ICommand致力於執行常規設置和顯示CustomUserControl

+0

是否想在'ComboBox'附近放置'CustomUserControl'來顯示'Popup'? – Maxim

+0

@Maxim我從來沒有與'彈出'工作,所以我不知道它是否適合我的情況。我只知道'CustomUserControl'是一個視圖,他的視圖模型必須顯示在組合框的附近,並且將其稱爲 –

+0

@Maxim Plus,我的問題是,在彈出框中,每次屬性IsOpened = true時都可以生成內容?由於CustomUserControl是基於哪個組合框按鈕啓動命令的不同內容的視圖 –

回答

1

你的要求是有點不清楚,但你可以使用一個Popup元素及其PlacementTarget到元素相結合,其中https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup.placementtarget(v=vs.110).aspx

然後可以使用在PopupContentControl及其Content屬性綁定到限定在彈出被dislayed內容返回一個視圖模型(或一個用戶控件)的性質:在打開時Popup被定位。類似這樣的:

<Button x:Name="btn" Content="Button" /> 
<Popup IsOpen="True" StaysOpen="True" 
     PlacementTarget="{Binding ElementName=btn}" 
     Placement="Top"> 
    <Border BorderThickness="1" Width="100" Height="100"> 
     <ContentControl Content="{Binding TheControlProperty}" /> 
    </Border> 
</Popup> 
+0

可以直接瀏覽彈出窗口的內容嗎? –

+0

如果你希望這是動態的,那麼TheControlProperty應該返回一個視圖。顯然,如果您希望能夠動態更新它,則無法在XAML標記中對視圖進行硬編碼。 – mm8

+0

完美。這就是我所需要的。 –