मैट हैमिल्टन के सुझाव के रूप में यूनिफॉर्मग्रिड का उपयोग करके इसका एक उदाहरण यहां दिया गया है।
सबसे पहले, कक्षाओं और डेटा को बनाने दें जो हम उपयोग करेंगे। प्रत्येक कार्ड एक कार्ड वस्तु द्वारा प्रतिनिधित्व किया जाएगा, और एक चेहरा संपत्ति है:
public class Card
{
public string Face { get; set; }
public Card() { }
}
इसके बाद, हम एक वर्ग कार्ड के बारे में हमारी संग्रह है, और यह भी एक संपत्ति हमें कार्ड की संख्या निर्धारित करने देता हैं की आवश्यकता होगी । कार्डकॉलेक्शन के लिए हम ObservableCollection का उपयोग कर सकते हैं क्योंकि कार्ड को जोड़ा या निकाला जाने पर स्वचालित रूप से यूआई को सूचित किया जाएगा। यूआई को सूचित करने के लिए नंबरऑफकार्ड्स संपत्ति को अपनी स्वयं की विधि की आवश्यकता होगी, इसके लिए हम implementINotifyPropertyChanged इंटरफ़ेस कर सकते हैं।
public class Cards : INotifyPropertyChanged
{
private int myNumberOfCards;
public int NumberOfCards
{
get { return this.myNumberOfCards; }
set
{
this.myNumberOfCards = value;
NotifyPropertyChanged("NumberOfCards");
// Logic is going in here since this is just an example,
// Though I would not recomend hevily modifying the setters in a finalized app.
while (this.myNumberOfCards > CardCollection.Count)
{
CardCollection.Add(new Card { Face = (CardCollection.Count + 1).ToString() });
}
while (this.myNumberOfCards < CardCollection.Count)
{
CardCollection.RemoveAt(CardCollection.Count - 1);
}
NotifyPropertyChanged("CardColumns");
}
}
public int CardColumns
{
get
{
return (int)Math.Ceiling((Math.Sqrt((double)CardCollection.Count)));
}
}
private ObservableCollection<Card> myCardCollection;
public ObservableCollection<Card> CardCollection
{
get
{
if (this.myCardCollection == null)
{ this.myCardCollection = new ObservableCollection<Card>(); }
return this.myCardCollection;
}
}
public Cards(int initalCards)
{
NumberOfCards = initalCards;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}
अंत में, हम विंडो में हमारे DataContext के रूप में सेट कर सकते हैं: हम यह भी एक संपत्ति है कि पंक्तियों/स्तंभों का उपयोग करने की संख्या का प्रतिनिधित्व करता है चाहता हूँ, यह सिर्फ हमारे NumberOfCards का वर्गमूल हो जाएगा , और एक्सएएमएल में हमारे कार्ड वर्ग से जुड़ें। एक्सएएमएल के लिए मैंने एक साधारण आइटम कंट्रोल का उपयोग किया, ताकि यह चयन योग्य न हो, और मैंने डेटा टेम्पलेट को एक बटन बनने के लिए सेट किया है, ताकि प्रत्येक कार्ड पर क्लिक किया जा सके, यह सब कुछ आवश्यक है!
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.DataContext = new Cards(25);
}
}
<Window x:Class="Sample_BoolAnimation.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="300"
Width="300">
<Grid>
<DockPanel>
<DockPanel DockPanel.Dock="Top">
<TextBlock Text="Number of Cards:" />
<TextBox Text="{Binding NumberOfCards, UpdateSourceTrigger=PropertyChanged}" />
</DockPanel>
<ItemsControl ItemsSource="{Binding CardCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="{Binding CardColumns}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Face}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DockPanel>
</Grid>
</Window>
एक और बात है कि मैं पर देख रहे हैं की सिफारिश करेंगे जोश स्मिथ के ContentControl3D कार्यान्वयन है। चूंकि यह आपको 'फ़्लिपिंग' व्यवहार दे सकता है जिसे आप कार्ड क्लास में काफी अच्छी तरह कार्यान्वित करने की तलाश में हैं।
बहुत अच्छा! +1 ... उम्मीद है कि यह "स्वीकार" हो जाता है! –
ओह यह कमाल है, बहुत बहुत धन्यवाद! –