Silverlight アイテムコントロール。テンプレートを使用してパネルを完全に削除できますか?
-
21-08-2019 - |
質問
ItemsControl 用の DataTamplate には、他のメタデータを含む画像が含まれているだけです。私がやろうとしているのは、ItemsControl にバインドし、指定したデータを介してバインドされた Convas.Left と Canvas.Top で画像を表示させることです。
親キャンバスで添付プロパティを使用できるように、ItemsPanelTemplate を介してコントロールからパネルを削除するよう最善を尽くしてきましたが、デフォルトでは常に StackPanel を取得するようです。
何か良いアイデアを持っている人はいますか?
ありがとう、デイブ
解決
ItemsControl 内の項目のレイアウトは、ItemsPanelTemplate 型の ItemsControl.ItemsPanel プロパティによって制御されます。ItemsControl.ItemsPanel プロパティのデフォルト値は、実際には StackPanel を指定する ItemsPanelTemplate のインスタンスですが、これは完全にカスタマイズ可能です。
コード例 (このMSDNページで)「次の例が項目制御を作成する」から始まる段落の下に示されています。 itemcontrol.template、itemcontrol.itemspanel、およびitemcontrol.itemtemplateプロパティが何であるかを理解するのに非常に役立ちます。
質問の最初の段落の 2 番目の文で説明されている内容を達成するには、いくつかの方法があります。完全な例を次に示します。
ページ.xaml:
<UserControl x:Class="ItemsControlImages.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:ItemsControlImages">
<UserControl.Resources>
<DataTemplate x:Key="CountryTemplate">
<Canvas>
<Image Canvas.Top="{Binding Location.Y}"
Canvas.Left="{Binding Location.X}"
Source="{Binding FlagImage}" />
<StackPanel Canvas.Top="{Binding Location.Y}"
Canvas.Left="{Binding Location.X}">
<TextBlock Text="{Binding Title}" />
<TextBlock Text="{Binding Location}" />
<StackPanel.RenderTransform>
<TranslateTransform Y="-32.0" />
</StackPanel.RenderTransform>
</StackPanel>
</Canvas>
</DataTemplate>
</UserControl.Resources>
<Canvas x:Name="LayoutRoot">
<Canvas.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFB2C6D5"/>
<GradientStop Color="#FF1483D9" Offset="1"/>
</LinearGradientBrush>
</Canvas.Background>
<ItemsControl ItemTemplate="{StaticResource CountryTemplate}">
<app:Country Title="Argentina" Location="56,218" FlagImage="Images/ARG.png" />
<app:Country Title="China" Location="368,66" FlagImage="Images/CHN.png" />
<app:Country Title="Ireland" Location="192,90" FlagImage="Images/IRE.png" />
<app:Country Title="New Zealand" Location="404,225" FlagImage="Images/NZ.png" />
<app:Country Title="United States" Location="40,80" FlagImage="Images/USA.png" />
</ItemsControl>
</Canvas>
</UserControl>
国.cs:
using System.ComponentModel;
using System.Windows;
namespace ItemsControlImages
{
public class Country : INotifyPropertyChanged
{
private string title;
private string flagImage;
private Point location;
public string Title
{
get
{
return this.title;
}
set
{
if ((object.ReferenceEquals(this.title, value) != true))
{
this.title = value;
this.RaisePropertyChanged("Title");
}
}
}
public string FlagImage
{
get
{
return this.flagImage;
}
set
{
if ((object.ReferenceEquals(this.flagImage, value) != true))
{
this.flagImage = value;
this.RaisePropertyChanged("FlagImage");
}
}
}
public Point Location
{
get
{
return this.location;
}
set
{
if ((this.location.Equals(value) != true))
{
this.location = value;
this.RaisePropertyChanged("Location");
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
この最終結果を得るために必要なのは (Images フォルダー内の画像とともに) それだけです。
代替テキスト http://www.freeimagehosting.net/uploads/bec683b75e.png
画像は ItemsControl 内にありますが、親 Canvas の Left および Top 添付プロパティをカスタム Location プロパティの X および Y 座標の値にバインドすることによって示される座標に配置されることに注意してください。
このサンプルの詳細と、一般的なテンプレートを使用した ItemsControl のカスタマイズについては、以下を参照してください。 このブログ投稿.