반응형
각각의 새 문자에서 WPF TextBox 바인딩을 실행합니까?
새 문자가 TextBox에 입력되는 즉시 데이터 바인딩 업데이트를 만들려면 어떻게해야합니까?
나는 WPF의 바인딩에 대해 배우고 있으며 이제 (희망적으로) 간단한 문제에 갇혀 있습니다.
Path 속성을 설정할 수있는 간단한 FileLister 클래스가 있으며 FileNames 속성에 액세스 할 때 파일 목록을 제공합니다. 그 수업은 다음과 같습니다.
class FileLister:INotifyPropertyChanged {
private string _path = "";
public string Path {
get {
return _path;
}
set {
if (_path.Equals(value)) return;
_path = value;
OnPropertyChanged("Path");
OnPropertyChanged("FileNames");
}
}
public List<String> FileNames {
get {
return getListing(Path);
}
}
private List<string> getListing(string path) {
DirectoryInfo dir = new DirectoryInfo(path);
List<string> result = new List<string>();
if (!dir.Exists) return result;
foreach (FileInfo fi in dir.GetFiles()) {
result.Add(fi.Name);
}
return result;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string property) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(property));
}
}
}
이 매우 간단한 앱에서 FileLister를 StaticResource로 사용하고 있습니다.
<Window x:Class="WpfTest4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest4"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:FileLister x:Key="fileLister" Path="d:\temp" />
</Window.Resources>
<Grid>
<TextBox Text="{Binding Source={StaticResource fileLister}, Path=Path, Mode=TwoWay}"
Height="25" Margin="12,12,12,0" VerticalAlignment="Top" />
<ListBox Margin="12,43,12,12" Name="listBox1" ItemsSource="{Binding Source={StaticResource ResourceKey=fileLister}, Path=FileNames}"/>
</Grid>
</Window>
바인딩이 작동 중입니다. 텍스트 상자의 값을 변경 한 다음 외부를 클릭하면 목록 상자 내용이 업데이트됩니다 (경로가 존재하는 한).
문제는 새 문자를 입력하자마자 업데이트하고 텍스트 상자가 초점을 잃을 때까지 기다리지 않는다는 것입니다.
어떻게 할 수 있습니까? xaml에서 직접이 작업을 수행하는 방법이 있습니까? 아니면 상자에서 TextChanged 또는 TextInput 이벤트를 처리해야합니까?
텍스트 상자 바인딩에서해야 할 일은 설정하는 것뿐입니다 UpdateSourceTrigger=PropertyChanged
.
UpdateSourceTrigger
속성을 다음과 같이 설정해야 합니다.PropertyChanged
<TextBox Text="{Binding Source={StaticResource fileLister}, Path=Path, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Height="25" Margin="12,12,12,0" VerticalAlignment="Top" />
갑자기 슬라이더와 관련 TextBox 간의 데이터 바인딩이 문제를 일으켰습니다. 드디어 이유를 찾아서 고칠 수있었습니다. 내가 사용하는 변환기 :
using System;
using System.Globalization;
using System.Windows.Data;
using System.Threading;
namespace SiderExampleVerticalV2
{
internal class FixCulture
{
internal static System.Globalization.NumberFormatInfo currcult
= Thread.CurrentThread.CurrentCulture.NumberFormat;
internal static NumberFormatInfo nfi = new NumberFormatInfo()
{
/*because manual edit properties are not treated right*/
NumberDecimalDigits = 1,
NumberDecimalSeparator = currcult.NumberDecimalSeparator,
NumberGroupSeparator = currcult.NumberGroupSeparator
};
}
public class ToOneDecimalConverter : IValueConverter
{
public object Convert(object value,
Type targetType, object parameter, CultureInfo culture)
{
double w = (double)value;
double r = Math.Round(w, 1);
string s = r.ToString("N", FixCulture.nfi);
return (s as String);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string s = (string)value;
double w;
try
{
w = System.Convert.ToDouble(s, FixCulture.currcult);
}
catch
{
return null;
}
return w;
}
}
}
XAML에서
<Window.Resources>
<local:ToOneDecimalConverter x:Key="ToOneDecimalConverter"/>
</Window.Resources>
추가로 정의 된 TextBox
<TextBox x:Name="TextSlidVolume"
Text="{Binding ElementName=SlidVolume, Path=Value,
Converter={StaticResource ToOneDecimalConverter},Mode=TwoWay}"
/>
참고 URL : https://stackoverflow.com/questions/10619596/making-a-wpf-textbox-binding-fire-on-each-new-character
반응형
'development' 카테고리의 다른 글
2D 배열을 반복하는 중첩 루프의 순서가 더 효율적입니다. (0) | 2020.10.26 |
---|---|
PHP 세션 기본 시간 초과 (0) | 2020.10.26 |
노드 / 익스프레스에서 사용자 지정 http 상태 메시지를 보내는 방법은 무엇입니까? (0) | 2020.10.26 |
pip를 사용하여 Pygame을 설치할 수 없습니다. (0) | 2020.10.26 |
오류-Android 리소스 연결 실패 (AAPT2 27.0.3 데몬 # 0) (0) | 2020.10.26 |