Posts

Showing posts with the label Wpf

Building A Great Dashboard App In WPF -- What Are The Controls Available Out There?

Answer : Check out the WPF Dashboard demo. It comes with source code. Also the WPF Dashboard project on codeplex. The Infragistics Dashboard demo is nice as well. My decision was to take the DragDockPanel from the Blacklight project. It's both WPF and Silverlight-enabled.

Collapse Grid Row In WPF

Answer : All you need is something to cache the height(s) of the visible row. After that, you no longer need converters or to toggle visibility of contained controls. CollapsibleRow public class CollapsibleRow : RowDefinition { #region Fields private GridLength cachedHeight; private double cachedMinHeight; #endregion #region Dependency Properties public static readonly DependencyProperty CollapsedProperty = DependencyProperty.Register("Collapsed", typeof(bool), typeof(CollapsibleRow), new PropertyMetadata(false, OnCollapsedChanged)); private static void OnCollapsedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if(d is CollapsibleRow row && e.NewValue is bool collapsed) { if(collapsed) { if(row.MinHeight != 0) { row.cachedMinHeight = row.MinHeight; row.MinHeight = 0; } ...

Changing The String Format Of The WPF DatePicker

Answer : I have solved this problem with a help of this code. Hope it will help you all as well. <Style TargetType="{x:Type DatePickerTextBox}"> <Setter Property="Control.Template"> <Setter.Value> <ControlTemplate> <TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, StringFormat='dd MMM yyyy', RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" /> </ControlTemplate> </Setter.Value> </Setter> </Style> It appears, as per Wonko's answer, that you cannot specify the Date format in Xaml format or by inheriting from the DatePicker. I have put the following code into my View's constructor which overrides the ShortDateFormat for the current thread: CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name); ci.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy"; Thread.CurrentThread.CurrentCu...

C# WPF - ScrollViewer + TextBlock Troubles

Answer : This works: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Name="Scroller"> <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="100" Width="{Binding ElementName=Scroller, Path=ViewportWidth}" TextWrapping="Wrap" Text="Some really long text that should probably wordwrap when you resize the window." /> </ScrollViewer> </Window> Without more detail, the best I can do is provide the standard way of doing this. Basically, host your element (which has a minimum size) in a scroll viewer; when th...

Click Event For WPF Image

Answer : Just add a MouseDown (or MouseLeftButtonDown as suggested) event to your image like so <Image x:Name=aPicture Source="mypic.jpg" MouseDown="aPicture_MouseDown"/> // or <Image x:Name=aPicture Source="mypic.jpg" MouseLeftButtonDown="aPicture_MouseDown"/> which should add this to your code behind private void aPicture_MouseDown(object sender, MouseEventArgs e) { //do something here } In WPF each control has its default template (how it looks) but you can easily change these templates and make controls look like you want. This makes it easier to pick control by its functionality and make it look like you want. In your case you want Click so you choose Button and change its Template <Window ...> <Window.Resources> <Style TargetType="{x:Type Button}" x:Key="ImageButtonStyle"> <Setter Property="Template"> <Setter.Value...

Change Image Source In Code Behind - Wpf

Answer : None of the above solutions worked for me. But this did: myImage.Source = new BitmapImage(new Uri(@"/Images/foo.png", UriKind.Relative)); You just need one line: ImageViewer1.Source = new BitmapImage(new Uri(@"\myserver\folder1\Customer Data\sample.png")); The pack syntax you are using here is for an image that is contained as a Resource within your application, not for a loose file in the file system. You simply want to pass the actual path to the UriSource: logo.UriSource = new Uri(@"\\myserver\folder1\Customer Data\sample.png");

Best Way To Make WPF ListView/GridView Sort On Column-header Clicking?

Answer : I wrote a set of attached properties to automatically sort a GridView , you can check it out here. It doesn't handle the up/down arrow, but it could easily be added. <ListView ItemsSource="{Binding Persons}" IsSynchronizedWithCurrentItem="True" util:GridViewSort.AutoSort="True"> <ListView.View> <GridView> <GridView.Columns> <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" util:GridViewSort.PropertyName="Name"/> <GridViewColumn Header="First name" DisplayMemberBinding="{Binding FirstName}" util:GridViewSort.PropertyName="FirstName"/> <GridViewColumn Header="Date of birth" ...

Cast Datagrid.SelectedItems Collection To List

Answer : Make sure to use the System.Linq namespace then : You should be able to use : List<Foo> SelectedItemsList = DataGrid.SelectedItems.Cast<Foo>().ToList(); or if you're not quite sure what DataGrid.SelectedItems contains : List<Foo> SelectedItemsList = DataGrid.SelectedItems.OfType<Foo>().ToList() Try this: DataGrid.SelectedItems.OfType<Foo>().ToList()

Bottom Borders On WPF Grid

Answer : On a Border control You can do BorderThickness="0 0 0 1" to only have a bottom border shown. Top and bottom border thickness of 5, left and right border thickness of 0 BorderThickness="0 5" Top and bottom border thickness of 0, left and right border thickness of 5 BorderThickness="5 0" Border Thickness - Left: 1, Top: 2, Right:3, Bottom: 4 BorderThickness="1 2 3 4" Hope this helps!

Change Button Background Color Through MVVM Pattern In WPF

Answer : You could bind the control's Background to a property on the viewmodel, the trick is to use an IValueConverter to return a Brush with the color you require. Here's an example that converts a boolean value from the viewmodel to a color: public class BoolToBrushConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) return Brushes.Transparent; return Convert.ToBoolean(value) ? Brushes.Red : Brushes.Transparent; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } with a binding expression like "{Binding Reviewed, Converter={StaticResource BoolToBrushConverter}}" where Reviewed is your boolean viewmodel property. Using triggers: <Button> <Button.Style> <Style Target...

Bind Command In WPF Using MVVM

Answer : You can bind the Command property of the button to any property that returns ICommand. Prism implements a nice convenient command called DelegateCommand that is very easy to use (here is a knock-off of it): public ICommand MyButtonClickCommand { get { return new DelegateCommand<object>(FuncToCall, FuncToEvaluate); } } private void FuncToCall(object context) { //this is called when the button is clicked } private bool FuncToEvaluate(object context) { //this is called to evaluate whether FuncToCall can be called //for example you can return true or false based on some validation logic return true; } <Button x:Name="myButton" Command="{Binding MyButtonClickCommand}" /> The CodeProject example How to use Commands in WPF has a very similar example with code that you can easily work through. The previous Stack Overflow question has an example using RoutedCommands that are statically bound to: How to bind Close command...

Aligning Content In A WPF Viewbox

Answer : Try VerticalAlignment="Top" and HorizontalAlignment="Left" on your viewbox. It will cause it to be anchored to the top and left side. <Grid> <Viewbox VerticalAlignment="Top" HorizontalAlignment="Left"> ... </Viewbox> </Grid> If you want it to completely fill (but keep it uniform) you can use Stretch="UniformToFill"

Change Margin Programmatically In WPF / C#

Answer : test.Margin = new Thickness(0, -5, 0, 0); Alignment, Margins and Padding Overview (MSDN) FrameworkElement.Margin (MSDN) test.Margin = new Thickness(0, 0, 0, 0); test.Margin = new Thickness(-5);

Binding Only Part Of The Margin Property Of WPF Control

Answer : Have you tried using a converter like this? in VB.Net Public Class MarginConverter Implements IValueConverter Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert Return New Thickness(0, CDbl(value), 0, 0) End Function Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack Return Nothing End Function End Class Or in C# public class MarginConverter : IValueConverter { public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) { return new Thickness(0, System.Convert.ToDouble(value), 0, 0); } public object ConvertBack(o...

Add A PDF Viewer To A WPF Application

Answer : As already suggested by @NawedNabiZada, one tried and straightforward way is to use embedded InternetExplorer to show Adobe PDF Reader ActiveX control. So it assumes you are running on Windows and have Adobe PDF Reader installed. Then you create a user control, window etc. that contains following control: <WebBrowser x:Name="pdfWebViewer"></WebBrowser> In the constructor navigate to blank page: pdfWebViewer.Navigate(new Uri("about:blank")); To load a PDF document to that control use this simple code: pdfWebViewer.Navigate(fullPathToPDF); This approach is used by many Windows software not only WPF apps including SAP client, but has a hidden problem, see this question. The Adobe PDF Reader Addon in Internet Explorer must be enabled for this to work. There are various problems with Acrobat Reader XI, better to use DC version. To enable Adobe PDF go to IE settings, add-ons and find Adobe PDF Reader and enable it (AR XI and above...

Align DataGrid Column Header To Center

Answer : Check this <DataGridTextColumn Header="Nombre" Binding="{Binding Nombre}"> <DataGridTextColumn.HeaderStyle> <Style TargetType="DataGridColumnHeader"> <Setter Property="HorizontalContentAlignment" Value="Center" /> </Style> </DataGridTextColumn.HeaderStyle> It should be StaticResource instead of DynamicResource in the Column: Style <Window.Resources> <Style x:Key="CenterGridHeaderStyle" TargetType="DataGridColumnHeader"> <Setter Property="HorizontalContentAlignment" Value="Center"/> </Style> </Window.Resources> Column <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" IsReadOnly="True" HeaderStyle="{StaticResource CenterGridHeaderStyle}"/> There is a response for doing it progr...

Automatic Vertical Scroll Bar In WPF TextBlock?

Answer : Wrap it in a scroll viewer: <ScrollViewer> <TextBlock /> </ScrollViewer> NOTE this answer applies to a TextBlock (a read-only text element) as asked for in the original question. If you want to show scroll bars in a TextBox (an editable text element) then use the ScrollViewer attached properties: <TextBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" /> Valid values for these two properties are Disabled , Auto , Hidden and Visible . can use the following now: <TextBox Name="myTextBox" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True">SOME TEXT </TextBox> Something better would be: <Grid Width="Your-specified-value" > <ScrollViewer> <TextBlock ...