简体   繁体   中英

WPF Binding between Control and Style

I have a control in my XAML bound to a property with a style.

<TextBox x:Name="txtCapAmount" Text="{Binding Path=CapAmount}" Style="{DynamicResource AmountTextBoxStyle}"  />

In the style I'd like to use the Binding from the control but am not sure how to set it

<Style x:Key="AmountTextBoxStyle" TargetType="{x:Type TextBox}">
    <EventSetter Event="TextBox.PreviewTextInput" Handler="PreviewTextInput_IsTheNewValueAnAcceptableAmount" />
    <EventSetter Event="CommandManager.PreviewExecuted" Handler="CommandPaste_IsTheNewValueAnAcceptableAmount" />
    <Setter Property="MinWidth" Value="100" />
    <Setter Property="Text" Value="{Binding Path=?????, TargetNullValue={x:Static System:String.Empty}, StringFormat={}{0:C0}}" />
</Style>

I want to reuse this style for multiple textboxes throughout the application but need to bind each individual textbox to a different property. In the style, is it possible alter the 'Text' property while retaining the Path set on the control?

You are using styles incorrectly here. A style should be used similar to CSS on the web, to alter the visual appearance of multiple instances of similar controls.

Why do you need the text in the style at all? You are already binding the text at the control instance level. Putting it in the style doesn't gain you anything, and as you have found just won't work

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