के साथ दोवे बाध्यकारी मैं एक उपयोगकर्ता नियंत्रण लिखने की कोशिश कर रहा हूं जिसमें आइटम नियंत्रण है, आइटम टेम्पलेट जिसमें टेक्स्टबॉक्स है जो दोवे बाध्यकारी की अनुमति देगा। हालांकि, मुझे अपने कोड में कहीं गलती करनी होगी, क्योंकि बाध्यकारी केवल काम करता है जैसे मोड = वनवे। यह अपने प्रोजेक्ट से बहुत सरल अंश है, लेकिन यह अभी भी समस्या होती है:आइटम्स कंट्रोल
<UserControl x:Class="ItemsControlTest.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid>
<StackPanel>
<ItemsControl ItemsSource="{Binding Path=.}"
x:Name="myItemsControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Mode=TwoWay,
UpdateSourceTrigger=LostFocus,
Path=.}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Click="Button_Click"
Content="Click Here To Change Focus From ItemsControl" />
</StackPanel>
</Grid>
</UserControl>
यहाँ ऊपर नियंत्रण के लिए कोड के पीछे है:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace ItemsControlTest
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public ObservableCollection<string> MyCollection
{
get { return (ObservableCollection<string>)GetValue(MyCollectionProperty); }
set { SetValue(MyCollectionProperty, value); }
}
// Using a DependencyProperty as the backing store for MyCollection. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyCollectionProperty =
DependencyProperty.Register("MyCollection",
typeof(ObservableCollection<string>),
typeof(UserControl1),
new UIPropertyMetadata(new ObservableCollection<string>()));
public UserControl1()
{
for (int i = 0; i < 6; i++)
MyCollection.Add("String " + i.ToString());
InitializeComponent();
myItemsControl.DataContext = this.MyCollection;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Insert a string after the third element of MyCollection
MyCollection.Insert(3, "Inserted Item");
// Display contents of MyCollection in a MessageBox
string str = "";
foreach (string s in MyCollection)
str += s + Environment.NewLine;
MessageBox.Show(str);
}
}
}
और अंत में, यहाँ मुख्य विंडो के लिए XAML है :
<Window x:Class="ItemsControlTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:ItemsControlTest"
Title="Window1" Height="300" Width="300">
<Grid>
<src:UserControl1 />
</Grid>
</Window>
ठीक है, यह सब कुछ है। मुझे यकीन नहीं है कि टेक्स्टबॉक्स को क्यों संपादित करना है। विंडो में गुण गुण पीछे कोड में बाइंडिंग के लिए स्रोत प्रॉपर्टी को अपडेट नहीं करते हैं, अर्थात् MyCollection। बटन पर क्लिक करने से समस्या मुझे चेहरे पर घूरने का कारण बनती है;) कृपया मुझे यह समझने में मदद करें कि मैं कहां गलत हूं।
Thanx!
एंड्रयू
यह महान चार्ली है, ऐसा लगता है कि आप इसे नाखुश करते हैं। आप सुझाव अच्छी तरह से काम कर रहे हैं! – Andrew