Created
March 28, 2013 18:31
-
-
Save AlbertoMonteiro/5265673 to your computer and use it in GitHub Desktop.
Texblock destinado a receber o nome de um arquivo que limita a quantidade de caracteres mas sempre mostra a extensão.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="WpfApplication1.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
Title="MainWindow" Height="350" Width="525"> | |
<Grid> | |
<Button Content="Selecionar" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="274,10,0,0" Click="Button_Click_1"/> | |
<Border HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Width="259" BorderBrush="Black" BorderThickness="1" > | |
<TextBlock x:Name="TxBArquivo" Text="Nenhum arquivo selecionado" Height="16"/> | |
</Border> | |
</Grid> | |
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Globalization; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Windows; | |
using System.Windows.Media; | |
using Microsoft.Win32; | |
namespace WpfApplication1 | |
{ | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
private void Button_Click_1(object sender, RoutedEventArgs e) | |
{ | |
var dlg = new OpenFileDialog(); | |
dlg.FileName = "Document"; | |
var showDialog = dlg.ShowDialog(); | |
if (showDialog.HasValue) | |
{ | |
var passouDoTamanho = false; | |
var extension = Regex.Match(dlg.FileName, @"[.]{1}(?<extension>[\w]+)$").Groups["extension"].Value; | |
var extensionSize = MeasureTextSize(extension, TxBArquivo.FontFamily, TxBArquivo.FontStyle, TxBArquivo.FontWeight, TxBArquivo.FontStretch, TxBArquivo.FontSize); | |
var dotSize = MeasureTextSize("....", TxBArquivo.FontFamily, TxBArquivo.FontStyle, TxBArquivo.FontWeight, TxBArquivo.FontStretch, TxBArquivo.FontSize); | |
var itextSize = new Size(extensionSize.Width + dotSize.Width, extensionSize.Height + dotSize.Height); | |
var sb = new StringBuilder(); | |
foreach (var @char in dlg.FileName) | |
{ | |
sb.Append(@char); | |
var format = string.Format("{0}....{1}", sb, extension); | |
itextSize = MeasureTextSize(format, TxBArquivo.FontFamily, TxBArquivo.FontStyle, TxBArquivo.FontWeight, TxBArquivo.FontStretch, TxBArquivo.FontSize); | |
if (TxBArquivo.ActualWidth < itextSize.Width) | |
{ | |
sb.Remove(sb.Length-1, 1); | |
passouDoTamanho = true; | |
break; | |
} | |
} | |
TxBArquivo.Text = passouDoTamanho ? string.Format("{0}....{1}", sb, extension) : dlg.FileName; | |
} | |
} | |
public static Size MeasureTextSize(string text, FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, FontStretch fontStretch, double fontSize) | |
{ | |
var typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch); | |
var ft = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, fontSize, Brushes.Black); | |
return new Size(ft.Width, ft.Height); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment