Mặc dù bạn có thể thiết lập miền địa phương chuyển đổi mặc định trong WPF theo đề nghị của Mamta Dalal nó là không đủ để chuyển đổi phím "thập phân" nhấn vào chuỗi chính xác. Mã này sẽ hiển thị các ký hiệu tiền tệ đúng đắn và định dạng ngày/giờ trên điều khiển dữ liệu-bound
//Will set up correct string formats for data-bound controls,
// but will not replace numpad decimal key press
private void Application_Startup(object sender, StartupEventArgs e)
{
//Among other settings, this code may be used
CultureInfo ci = CultureInfo.CurrentUICulture;
try
{
//Override the default culture with something from app settings
ci = new CultureInfo([insert your preferred settings retrieval method here]);
}
catch { }
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
//Here is the important part for databinding default converters
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(FrameworkElement),
new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(ci.IetfLanguageTag)));
//Other initialization things
}
tôi thấy rằng xử lý PreviewKeyDown kiện cửa sổ rộng là một chút sạch hơn textbox cụ thể (sẽ tốt hơn nếu nó có thể được áp dụng rộng rãi).
public partial class MainWindow : Window
{
public MainWindow()
{
//Among other code
if (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator != ".")
{
//Handler attach - will not be done if not needed
PreviewKeyDown += new KeyEventHandler(MainWindow_PreviewKeyDown);
}
}
void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Decimal)
{
e.Handled = true;
if (CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator.Length > 0)
{
Keyboard.FocusedElement.RaiseEvent(
new TextCompositionEventArgs(
InputManager.Current.PrimaryKeyboardDevice,
new TextComposition(InputManager.Current,
Keyboard.FocusedElement,
CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator)
) { RoutedEvent = TextCompositionManager.TextInputEvent});
}
}
}
}
Nếu ai có thể đưa ra một cách để thiết lập nó ứng dụng rộng ...
Nguồn
2012-12-05 10:50:16
Cảm ơn bạn đã gợi ý ... Tôi cũng tìm thấy một bài viết trên blog đó giúp tôi: http: //www.metanous .be/pharcyde/post/Sử dụng-số-bàn phím-số thập phân-phím-như-số-dấu phân cách-cho-tất-cả-miền---trong-Excel.aspx –
LukePet, liên kết được khoanh vùng bị hỏng, xin vui lòng bạn có thể trả lời chi tiết hơn không. Cảm ơn. – sthiers
Dành cho người dùng VB.NET: https://social.msdn.microsoft.com/Forums/vstudio/en-US/4bedf3c3-be4f-4b2a-b5e0-21c1a41caeca/how-to-use-decimal-separator-in-a -wpf-textbox-? forum = wpf – Naucle