मैं यहां दो अन्य उत्तरों से सहमत नहीं हूं। सामग्री को लपेटने के लिए ग्रिड को जोड़ने की आवश्यकता नहीं है। Stackpanel पर्याप्त है।
xaml में एक स्टैकपैन जोड़ें जहां आपको सामग्री की आवश्यकता है।
<StackPanel Name="myStack" Orientation="Horizontal"></StackPanel>
तो कोड के पीछे में, एक बटन हैंडलर में या जब की तरह विंडो लोड जोड़ने के इस
Image coolPic = new Image() {
Name="pic",
Source = new BitmapImage(new Uri("pack://application:,,,/images/cool.png")),
Stretch = Stretch.None // this preserves the original size, fill would fill
};
TextBlock text = new TextBlock() {
Name = "myText",
Text = "This is my cool Pic"
};
myStack.Children.Add(coolPic); // adding the pic first places it on the left
myStack.Children.Add(text); // the text would show up to the right
आप पहली बार तब पाठ जोड़कर छवि के स्थान और पाठ स्वैप कर सकते हैं छवि।
यदि आप छवि को नहीं देखते हैं कि छवि की निर्माण कार्य छवि की गुण विंडो में संसाधन पर सेट है।
कोड को और अधिक उपयोगी और अधिक गतिशील होने के लिए आपको टेक्स्ट या छवि को बदलने का एक तरीका चाहिए।
तो मान लीजिए कि आपने उन बदलने के लिए करना चाहता था और आप आगे बढ़ कर एक
((TextBlock)FindName("myText")).Text = "my other cool pic";
आप उम्मीद करेंगे पाठ परिवर्तित करने की है, लेकिन क्या होता है?
Object reference not set to an instance of an object.
ड्रैट्स लेकिन मैंने इसे एक नाम दिया। आपको
// register the new control
RegisterName(text.Name, text);
जोड़ने की आवश्यकता होगी ताकि आप बाद में टेक्स्टब्लॉक तक पहुंच सकें। इसकी आवश्यकता है क्योंकि आपने इसे बनाया और प्रदर्शित करने के बाद ढांचे पर नियंत्रण जोड़ा।इसलिए अंतिम कोड छवि को पंजीकृत करने के बाद भी ऐसा लगता है
Image coolPic = new Image() {
Name="pic",
Source = new BitmapImage(new Uri("pack://application:,,,/images/cool.png")),
Stretch = Stretch.None // this preserves the original size, fill would fill
};
// register the new control
RegisterName(coolPic.Name, coolPic);
TextBlock text = new TextBlock() {
Name = "myText",
Text = "This is my cool Pic"
};
// register the new control
RegisterName(text.Name, text);
myStack.Children.Add(coolPic);
myStack.Children.Add(text);
ग्रीट उत्तर !! – CharlieShi