2012-02-29 12 views
1

私は現在、実行時に自分のWPFアプリケーションでカスタムラベルコントロールを動的に追加しています。WPFでコントロールを「アンラップ」する方法はありますか?

必要に応じてラベルの内容をラップするWrapPanelがあります(内容は動的に設定されています)。

すべて正常に動作しますが、ラベルを「アンラップ」します。

このラベルは、画面上の他の場所でクリックしてドラッグすることができます(ドラッグアンドドロップ機能は問題ありません)。これははるかに広い領域であるため、1行だけを占有する必要があります。

この種の機能を実現するにはどうすればよいでしょうか?

答えて

0

最も簡単な方法これは、動的リストのItemsControlを使用して、そのItemsPanelTemplateをオンザフライで変更することです。

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfApplication1" 
    Title="MainWindow" 
    Height="350" 
    Width="200"> 
<Window.Resources> 
    <ItemsPanelTemplate x:Key="wrapPanelTemplate"> 
     <WrapPanel /> 
    </ItemsPanelTemplate> 
    <ItemsPanelTemplate x:Key="stackPanelTemplate"> 
     <StackPanel /> 
    </ItemsPanelTemplate> 
</Window.Resources> 
<StackPanel> 
    <ItemsControl x:Name="itemsControl" 
        ItemsPanel="{StaticResource wrapPanelTemplate}"> 
     <ListBoxItem Content="TESTTEST " /> 
     <ListBoxItem Content="TESTTEST " /> 
     <ListBoxItem Content="TESTTEST " /> 
     <ListBoxItem Content="TESTTEST " /> 
    </ItemsControl> 
    <Button Click="Button_Click" Content="Change Template Button"/> 
</StackPanel> 

MainWindow.xaml.cs:

namespace WpfApplication1 
{ 
public partial class MainWindow : Window 
{ 
    ItemsPanelTemplate stackPanelTemp; 
    public MainWindow() 
    { 
     InitializeComponent(); 
     stackPanelTemp = (ItemsPanelTemplate)(this.FindResource("stackPanelTemplate")); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     itemsControl.ItemsPanel = stackPanelTemp; 
    } 
} 
} 

・ホープ、このことができますここで完全に動作するコードは、私だけで作られた...

MainWindow.xamlです!

+0

ありがとうございます!一度に1つのラベルだけにするにはどうすればいいですか? – Lucas

+0

MVVMパターンをアプリケーションで使用していますか? –

+0

ええ、Caliburn.Microの助けを借りてMVVMを使用しています。 – Lucas

関連する問題