简体   繁体   中英

Make a binding on ElementName to change when trigered

I'm probably missing something because what I do is suposed to work I think. I'm trying to bind the ElementName of a value to change when trigered.

here's the code (it is in the style):

<ContentPresenter>
  <ContentPresenter.Style>
    <Style>
      <Style.Triggers>
        <DataTrigger Binding="{Binding Tick}" Value="True">
          <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName={Binding FieldNameFocus}}"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </ContentPresenter.Style>
</ContentPresenter>

And get this error:

Error   2   A 'Binding' cannot be set on the 'ElementName' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.   C:\Users\xavier\Documents\Visual Studio 2012\Projects\Test_Validation\Test_Validation\MainWindow.xaml   1   2   Test_Validation

Maybe I'm not in the right path though... I just want to have focusedElement to change according to FieldNameFocus when Tick becomes True. (Both are in my dataContext)

Thanks in advance.

Your binding should look like this: Value="{Binding ElementName=YourElementName, Path=PropertyOfTheElement}"

Example binding by ElementName:

<TextBox Name="tbx1" Text="TextBox"/>

<TextBox Name="tbx2" Text="{Binding ElementName=tbx1, Path=Text}"/>

Hello the error is pretty clear, you cannot use Binding on with ElementName. When using ElementName you should provide the name of a control present in your xaml File as the previous post showed. As your trigger is bind to a boolean value you might want to replace "ElementName={Binding FieldNameFocus}" by "ElementName=controlNameOnWhichYouWantToSetFocus" .

Eg <Textbox x:Name="ControlIWantFocusOnWhenTickIstrue"> then in your trigger

<Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName="ControlIWantFocusOnWhenTickIstrue"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM