Display control text based on gotfocus of another control

I am new to WPF development.

I am developing a wpf application using the MVVM pattern. I had "ComboBox" and "TextBlock" controls. When receiving focus over the ComboBox, Textblock should display the ComboBox prompt. Combobox is tied to view the model.

<ComboBox Name="cmbSystemVoltage" 
          ToolTip="RMS value of phase-phase voltage in kV" 
          ItemsSource="{Binding Path=SystemVoltageStore}"
          SelectedItem="{Binding Path=SelectedSystemVoltage}" 
          DisplayMemberPath="SystemVoltageLevel"/>

How can i achieve this. Sample code for this will be very useful.

Thanks Sudhi

+3
source share
1 answer

Use DataTriggerand tie for ElementName:

<StackPanel>
    <TextBlock>
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">                   
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=cmbSystemVoltage, Path=IsKeyboardFocusWithin}"
                                 Value="True">
                        <Setter Property="Text"
                                Value="{Binding ElementName=cmbSystemVoltage, Path=ToolTip}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    <ComboBox Name="cmbSystemVoltage" ToolTip="RMS value of phase-phase voltage in kV" />
</StackPanel>

EDIT

If you want to show a hint of several controls in TextBlock, I would rather subscribe to PreviewGotKeyboardFocus Event:

<Window PreviewGotKeyboardFocus="Window_PreviewGotKeyboardFocus">
    <StackPanel>
        <TextBlock x:Name="toolTipIndicator" />
        <ComboBox ToolTip="Sample text" />
        <TextBox ToolTip="Other sample text" />
    </StackPanel>
</Window>

.

void Window_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    FrameworkElement element = e.NewFocus as FrameworkElement;

    if (element != null && element.ToolTip != null)
    {
        this.toolTipIndicator.Text = element.ToolTip.ToString();
    }
    else
    {
        this.toolTipIndicator.Text = string.Empty;
    }
}
+2
source

All Articles