추가 할 수있는 도움말 항목의 이름과 주제 이름이 변경된 목록 상자가 있습니다.Silverlight 컨트롤이 표시되었음을 어떻게 알 수 있습니까?
XAML : : 원래 그냥 문자열을 표시하지만, 인라인 편집 내가 TextBlock
또는 TextBox
를 표시할지 여부를 결정할 수있는 문자열로 구성된 사용자 정의 유형 및 UI 그래서 InEdit
속성을 사용하도록 변경 작업을 진행하게되었다
<ListBox ItemsSource="{Binding HelpTopics, Mode=TwoWay}"
SelectedValuePath="Description"
SelectedValue="{Binding SelectedPageId, Mode=TwoWay}"
SelectionChanged="ListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Description, Mode=TwoWay}"
VerticalAlignment="Center"
MouseLeftButtonUp="TopicTextBlock_MouseLeftButtonUp"
Visibility="{Binding InEdit, Converter={StaticResource boolToVisibilityConverter}, ConverterParameter=contra}"/>
<TextBox Text="{Binding Description, Mode=TwoWay}"
Visibility="{Binding InEdit, Converter={StaticResource boolToVisibilityConverter}, ConverterParameter=pro}"
LostFocus="EditTopicTextBox_LostFocus"
HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Margin="5" Content="Add Topic" Command="{Binding AddTopicCommand}"/>
HelpTopics
은 ObservableCollection<EditableHelpTopic>
입니다.
SelectedPageId
은 입니다.
boolToVisibilityConverter
은 그것이 말하는 것을 수행하는 변환기입니다. 어떤 작품
:
- 이 항목을 추가 새로운 아이템을 생성하고 목록에 추가하고 편집 모드에서 항목을 넣어.
- 기존 항목을 두 번 클릭하면 해당 항목이 편집 모드로 전환되고
TextBox
에 포커스가 설정되고 덮어 쓸 수 있도록 모든 텍스트가 선택됩니다. TextBox
이 포커스를 잃으면 편집 내용이 저장되고 디스플레이는TextBlock
으로 돌아갑니다.
작동하지 않습니다 무엇 :
- 새로운 항목이 추가되면
TextBox
사용자가 새 이름을 입력 할 수 있도록 초점과 텍스트를 선택해야한다.
그래서 제 질문은 내가 TextBox
이 만들어 졌는지 알고 내가 포커스를 설정하고 그 내용을 선택할 수 있도록 볼 수있는 코드 나 이벤트의 포인트가된다. SelectionChanged
이벤트에 연결하려고 시도했지만 TextBox
이벤트가 아직 표시되지 않았습니다. 또한 뷰에서 처리 한 뷰 모델의 OnAddTopicExecute
메서드에 이벤트를 추가했지만 TextBox
이 표시되기 전에 다시 발생했습니다.
아래는 위의 XAML을 지원하는 코드입니다.
private DateTime lastClickTime = DateTime.MinValue;
private Point lastClickPosition;
private void TopicTextBlock_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
UIElement element = sender as UIElement;
if ((DateTime.Now - this.lastClickTime).TotalMilliseconds > 300)
{
this.lastClickPosition = e.GetPosition(element);
this.lastClickTime = DateTime.Now;
}
else
{
Point position = e.GetPosition(element);
if (Math.Abs(this.lastClickPosition.X - position.X) < 4 && Math.Abs(this.lastClickPosition.Y - position.Y) < 4)
{
var textBlock = sender as TextBlock;
var editableHelpTopic = textBlock.DataContext as EditableHelpTopic;
editableHelpTopic.InEdit = true;
var parent = textBlock.Parent as Grid;
TextBox textBox = parent.Children.First(c => c.GetType() == typeof(TextBox)) as TextBox;
textBox.Focus();
textBox.SelectAll();
}
}
}
private void EditTopicTextBox_LostFocus(object sender, RoutedEventArgs e)
{
var textBox = sender as TextBox;
var editableHelpTopic = textBox.DataContext as EditableHelpTopic;
editableHelpTopic.InEdit = false;
if (!textBox.Text.Equals(editableHelpTopic.Description))
{
this.editViewModel.RenameTopic(textBox.Text);
}
}
보기 모델 :
코드 뒤에) 나는 그것을 줄이려고 노력했지만 여전히 많은있을 것 같습니다, 그래서 당신이하지 관심이 있다면 당신은이를 건너 뛸 수 있습니다했습니다 DelegateCommand
가 ICommand
의 구현이다
public EditViewModel()
{
...
this.AddTopicCommand = new DelegateCommand(this.OnAddTopicExecute, this.OnAddTopicCanExecute);
...
}
.
private void OnAddTopicExecute(object parameter)
{
var newTopic = new EditableHelpTopic
{
Description = "NewTopic",
InEdit = true
};
this.HelpTopics.Add(newTopic);
this.SelectedPageId = newTopic.Description;
}
정의 :
public class EditableHelpTopic : INotifyPropertyChanged
{
public bool InEdit { ... }
public string Description { ... }
}
+1 크리스이 http://stackoverflow.com/questions/124649/how-do- i-give-a-textbox-focus-in-silverlight)는 장기간에 걸리는 두통을 해결했습니다 :) – Town