[英]Can a DependencyProperty bound two times to a TwoWay binding?
I have a Control that displays something (let's call it Display
). 我有一个显示某些内容的控件(我们称其为
Display
)。 In this control I have a class Camera
that stores things like zoom, position and rotation. 在此控件中,我有一个
Camera
类,用于存储缩放,位置和旋转之类的内容。
I can change the zoom from an external control (let's call it ZoomBar
). 我可以从外部控件更改缩放
ZoomBar
(我们将其ZoomBar
)。
Now I had the idea to connect all of them with a TwoWay-Binding like this: 现在,我有了将它们都通过TwoWay-Binding连接的想法,如下所示:
ZoomBar.Value <--> Display.Zoom <--> Camera.Zoom
It should be like: ZoomBar
value changes --> update Display.Zoom
--> update Camera.Zoom
. 它应该像:
ZoomBar
值更改->更新Display.Zoom
>更新Camera.Zoom
。 Display.Zoom
does not really do something. Display.Zoom
并没有真正做任何事情。 It's only for exchange the data between Camera
and ZoomBar
. 仅用于在
Camera
和ZoomBar
之间交换数据。
But I get nothing. 但是我什么也没得到。 After a short check in the Camera:
短暂检查一下相机后:
public float Zoom
{
get { MessageBox.Show("Any calls here?"); return (float)GetValue(ZoomProperty); }
set { ... }
}
I get a massive amount of MessageBoxes. 我得到了大量的MessageBoxes。 I guess there is something like loop in there.
我猜那里有些循环。 Like
ZoomBar.Value
--> Display.Zoom
--> ZoomBar.Value
--> ... 像
ZoomBar.Value
> Display.Zoom
> ZoomBar.Value
> ...
Are the two-way bindings causing the problem and if it is the bindings, is there a XAML way to fix this? 双向绑定是否会引起问题,如果是绑定,是否有XAML方法可以解决此问题?
<StatusBarItem Title="Zoom Bar" HorizontalAlignment="Right">
<Slider x:Name="uxInputZoom" Style="{DynamicResource ZoomSliderStyle}" Value="100" Maximum="500" Minimum="20" />
</StatusBarItem>
<Display x:Name="uxDisplay" Zoom="{Binding Value, Converter={StaticResource PercentToFractionConverter}, ElementName=uxInputZoom, Mode=TwoWay}" />
public static readonly DependencyProperty ZoomProperty = DependencyProperty.Register("Zoom", typeof(float), typeof(Display), new FrameworkPropertyMetadata(1f));
public float Zoom
{
get { return (float)GetValue(ZoomProperty); }
set { SetValue(ZoomProperty, value); }
}
Camera _camera = new Camera();
//...
public Display()
{
Binding binding = new Binding("Zoom");
binding.Source = _camera;
binding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(this, Display.ZoomProperty, binding);
InitializeComponent();
...
}
You certainly can data bind one property value to more than one UI control. 您当然可以将一个属性值绑定到多个UI控件上。 Take this simple example which enables movements of the
Slider
to update the value in the TextBox
, while also enabling values entered in the TextBox
to update the Slider.Value
property: 以这个简单的示例为例,该示例使
Slider
移动能够更新TextBox
的值,同时还使在TextBox
输入的值能够更新Slider.Value
属性:
<StackPanel>
<Slider Value="{Binding Width2}" Minimum="0.0" Maximum="100.0" Margin="0,0,0,20" />
<TextBox Text="{Binding Width2, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
This will not cause any feedback loops as in your code, so I suspect that you have something else doing that. 这不会像您的代码中那样导致任何反馈循环,因此我怀疑您还有其他事情要做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.