如何实现WPF Slider和Textbox的双向绑定?

huangapple go评论113阅读模式
英文:

How to WPF Slider and Textbox two way binding?

问题

我想将 slider 的值绑定到 textbox,并将 textbox 的值绑定到 slider 的值(用户交互)。
目前只能将 textbox 绑定到 slider 的值。如何实现双向绑定?

  1. <TextBox
  2. Width="70"
  3. Height="20"
  4. Grid.Row="1"
  5. Margin="0 0 80 180"
  6. x:Name="AO1text"
  7. Text="{Binding ElementName=AOch1, Path=Value, StringFormat={}{0:F2}}"/>
  1. <Slider
  2. x:Name="AOch1"
  3. Maximum="20"
  4. Minimum="4"
  5. Value="{Binding ao1value, Mode=TwoWay}"
  6. Width="250"
  7. BorderThickness="5"
  8. TickPlacement="BottomRight"

不要回答我要翻译的问题。

英文:

i want slider value to binding on textbox and textbox to slider value(user interaction).
im now just textbox binded of slider value. How can i get it?

  1. <TextBox
  2. Width="70"
  3. Height="20"
  4. Grid.Row="1"
  5. Margin="0 0 80 180"
  6. x:Name="AO1text"
  7. Text="{Binding ElementName=AOch1,Path=Value, StringFormat={}{0:F2}}"/>
  1. <Slider
  2. x:Name="AOch1"
  3. Maximum="20"
  4. Minimum="4"
  5. Value="{Binding ao1value}"
  6. Width="250"
  7. BorderThickness="5"
  8. TickPlacement="BottomRight"

答案1

得分: -1

  1. <TextBox Width="120" Text="{Binding ElementName=AOch1, Path=Value, StringFormat={}{0:N0}, UpdateSourceTrigger=PropertyChanged}"/>
  2. <Slider x:Name="AOch1" Maximum="100" Minimum="0" Width="250" />
  1. // 从滑块获取值
  2. targetObject.Value = AOch1.Value;

建议的方法是:

  1. 创建一个具有属性(例如 Value)的类
  1. public class ItemModel
  2. {
  3. double _Value = 4;
  4. public double Value
  5. {
  6. get => _Value;
  7. set
  8. {
  9. if (value < 4)
  10. value = 4;
  11. if (value > 20)
  12. value = 20;
  13. if (_Value != value)
  14. {
  15. _Value = value;
  16. // 通知属性更改
  17. }
  18. }
  19. }
  20. }
  21. // Window1.xaml.cs
  22. ItemModel model = new ItemModel();
  23. // Window1_Loaded
  24. yourGrid.DataContext = model;
  1. 将 ItemModel.Value 绑定到 TextBox.Text 和 Slider.Value。
  1. &lt;TextBox Text=&quot;{Binding Path=Value, StringFormat={}{0:F2}, UpdateSourceTrigger=PropertyChanged}&quot;/&gt;
  2. &lt;Slider Value=&quot;{Binding Path=Value}&quot; Maximum=&quot;20&quot; Minimum=&quot;4&quot; Width=&quot;250&quot; BorderThickness=&quot;5&quot; TickPlacement=&quot;BottomRight&quot; /&gt;
  1. 这样,在代码部分中,您只需要访问 ItemModel.Value。
英文:

try this

  1. &lt;TextBox Width=&quot;120&quot; Text=&quot;{Binding ElementName=AOch1, Path=Value, StringFormat={}{0:N0}, UpdateSourceTrigger=PropertyChanged}&quot;/&gt;
  2. &lt;Slider x:Name=&quot;AOch1&quot; Maximum=&quot;100&quot; Minimum=&quot;0&quot; Width=&quot;250&quot; /&gt;
  1. // Get value from Slider
  2. targetObject.Value = AOch1.Value;

The recommended approach is:

  1. Create a class with property, like Value
  1. public class ItemModel
  2. {
  3. double _Value = 4;
  4. public double Value
  5. {
  6. get =&gt; _Value;
  7. set
  8. {
  9. if (value &lt; 4)
  10. value = 4;
  11. if (value &gt; 20)
  12. value = 20;
  13. if (_Value != value)
  14. {
  15. _Value = value;
  16. // Notify Property Changed
  17. }
  18. }
  19. }
  20. }
  21. // Window1.xaml.cs
  22. ItemModel model = new ItemModel();
  23. // Window1_Loaded
  24. yourGrid.DataContext = model;
  1. Bind ItemModel.Value to the TextBox.Text and Slider.Value.
  1. &lt;TextBox Text=&quot;{Binding Path=Value, StringFormat={}{0:F2}, UpdateSourceTrigger=PropertyChanged}&quot;/&gt;
  2. &lt;Slider Value=&quot;{Binding Path=Value}&quot; Maximum=&quot;20&quot; Minimum=&quot;4&quot; Width=&quot;250&quot; BorderThickness=&quot;5&quot; TickPlacement=&quot;BottomRight&quot; /&gt;
  1. In this way, in the code section, you only need to access ItemModel.Value.

huangapple
  • 本文由 发表于 2023年6月29日 08:34:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定