我有一个UserControl LetterInRowUserControl与LongListSelector和ItemTemplate在xaml部分
<UserControl>
...
<UserControl.Resources>
<DataTemplate x:Key="itemTemplate">
<local:LetterControl x:Name="letterControl" VerticalAlignment="Top" Width="50" Letter="{Binding}"/>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<phone:LongListSelector x:Name="lls" LayoutMode="List" ItemTemplate="{StaticResource itemTemplate}" />
</Grid>
</UserControl>
cs part:
public partial class LetterInRowUserControl : UserControl
{
public LetterInRowUserControl()
{
InitializeComponent();
LetterControl.TextChanged += (o, ev) =>
{
var conv = new StringToListConverter();
ContainsLetters = (string)conv.ConvertBack(lls.ItemsSource, typeof(string), null, CultureInfo.CurrentCulture);
};
}
public static readonly DependencyProperty ContainsLettersProperty =
DependencyProperty.Register("ContainsLetters", typeof (string), typeof (LetterInRowUserControl), new PropertyMetadata(default(string),
(o, args) =>
{
var control = (LetterInRowUserControl) o;
var conv = new StringToListConverter();
control.lls.ItemsSource = (ObservableCollection<string>)conv.Convert(args.NewValue, typeof(string), null, CultureInfo.CurrentCulture);
}));
public string ContainsLetters
{
get
{
return (string) GetValue(ContainsLettersProperty);
}
set
{
SetValue(ContainsLettersProperty, value);
}
}
}
然后我有第二个Usercontrol - LetterControl,它在LetterInRowUserControl的itemtemplate里面
xaml part:
<UserControl>
...
<Grid x:Name="LayoutRoot">
<Grid>
<Button x:Name="hidden" Height="0" Width="0"/>
<TextBox x:Name="textBox"
TextWrapping="Wrap" VerticalAlignment="Top" Height="84" Margin="-12" FontFamily="Segoe WP Semibold" FontSize="33.333" TextAlignment="Center" GotFocus="textBox_GotFocus" LostFocus="textBox_LostFocus" MaxLength="1" TextChanged="textBox_TextChanged"/>
</Grid>
</Grid>
</UserControl>
和cs part:
public partial class LetterControl : UserControl
{
public static event TextChangedEventHandler TextChanged;
public static readonly DependencyProperty LetterProperty =
DependencyProperty.Register("Letter", typeof(string), typeof(LetterControl), new PropertyMetadata(default(string),
(o, args) =>
{
var control = (LetterControl)o;
control.textBox.Text = (string)args.NewValue;
}));
public string Letter
{
get { return (string)GetValue(LetterProperty); }
set
{
SetValue(LetterProperty, value);
}
}
public LetterControl()
{
InitializeComponent();
}
private void textBox_GotFocus(object sender, RoutedEventArgs e)
{
textBox.SelectAll();
}
private void textBox_LostFocus(object sender, RoutedEventArgs e)
{
textBox.Text = textBox.Text.ToUpper();
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
SetValue(LetterProperty,textBox.Text);
if(textBox.Text.Length>=1)
hidden.Focus();
if (TextChanged != null) TextChanged.Invoke(sender, e);
}
}
基本上有两个UserControl:LetterInRowUserControl和LetterControl。
LetterInRowUserControl在其ItemTemplate中包含带有LetterControl的LongListSelector。
问题在于,如果我从LetterControl内部更改Property Letter,它不会影响LetterInRowUserControl中LongListSelector中的ItemSource。相反(如果我直接更改ItemSource)它没有问题。
/打算用于一个Windows Phone 8应用程序
...类似于一个字母的文本框的列表框 - 问题是如果我编辑一个字母列表框的ItemSource没有改变。
编辑:经过努力尝试,我成功了。我的代码正在运行。主要问题是我使用的是ObservableCollection<string>
而不是ObservableCollection<CustomObjectWithINotifyPropChangedImplementation>.
所以该值并未自动更改(ObservableCollection未实现其功能)。