User Tools

Site Tools


technical_notes:public:20131018-wpf-faq

Parent

WPF - FAQ

Interaction

How to set input key (enter for example) inside a container ?

<StackPanel Background="Transparent">
  <StackPanel.InputBindings>
 
    <KeyBinding Command="{Binding ChangeColorCommand}"
                CommandParameter="{Binding ElementName=colorPicker, Path=SelectedItem}"
                Key="{Binding ChangeColorCommand.GestureKey}"
                Modifiers="{Binding ChangeColorCommand.GestureModifier}"/>
 
  </StackPanel.InputBindings>
 
  <ListBox Name="colorPicker"
           Background="Transparent"
           xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String>Red</sys:String>
    <sys:String>Green</sys:String>
  </ListBox>
</StackPanel>

Sources : http://msdn.microsoft.com/fr-fr/library/system.windows.input.inputbinding.aspx

Datagrid

How to set image in a column datagrid ?

<DataGrid ItemsSource="{Binding Customers}" AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Image" Width="SizeToCells" IsReadOnly="True">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Image Source="{Binding Image}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

How to set specific binding to a column DataGridTemplateColumn ?

I don't find any solution.

Explanation : http://keepitsimpleengineer.blogspot.fr/2010/09/wpf-datagrid-columns-are-not-part-of.html

Sources : http://www.wpftutorial.net/DataGrid.html

How to have a textblock that adapt this size to a column ?

<DataGridTemplateColumn Header="Personne">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock DataContext="{Binding Personne}" HorizontalAlignment="Left" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Binding

How to pass Converter param that is another type of String ?

<Button Grid.Column="8" Grid.Row="2" Content="Rechercher"
                            Style="{StaticResource SearchButton}">
    <Button.IsEnabled>
        <Binding Path="IsBusy"
                 Converter="{StaticResource EqualsConverter}">
            <Binding.ConverterParameter>
                <sys:Boolean>False</sys:Boolean>
            </Binding.ConverterParameter>
        </Binding>
    </Button.IsEnabled>
</Button>

Sources : How to use a ConverterParameter which is not a string in XAML? (by Matthieu MEZIL)

How to get the parent of a control ?

{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}, Path=Name, Mode=TwoWay}

How to bind on the property Validation.HasError with a DataTrigger ?

<Style.Triggers>
  <DataTrigger Binding="{Binding Path=(Validation.HasError), RelativeSource={RelativeSource AncestorType={x:Type c:DatePicker}}" Value="false">
      <Setter Property="Visibility" Value="Collapsed" />
  </DataTrigger>
</Style.Triggers>

Sources :

TextBlock

How to wrap a text block too long using … ?

Use the property TextTrimming.

How to make TextBlock field selectable ?

You have to use a TextBox with the following style. Be careful with the Binding, you can only use the OneWay

<Style x:Key="TextBlockSelectable" TargetType="TextBox">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="IsReadOnly" Value="True" />
    <Setter Property="TextWrapping" Value="Wrap" />
</Style>
technical_notes/public/20131018-wpf-faq.txt · Last modified: 2013/11/07 14:20 by Fabien Arcellier