简体   繁体   English

将 Xaml UserControl 视图绑定到它的代码后面,但后面的代码绑定到其他 object

[英]Binding Xaml UserControl view to it's code behind, but code behind bound to other object

I have a UserControl defined by Xaml and code behind.我有一个由 Xaml 定义的 UserControl 和后面的代码。 It should update as the model on which it is based changes.它应该随着它所基于的 model 的变化而更新。

I have constructed each instance of these controls in code, and set up bindings to some object _interestingSystem .我在代码中构建了这些控件的每个实例,并设置了与一些 object _interestingSystem的绑定。

var newViewInstance = new BroadcastCell
{
    HeartbeatStatus = Heartbeat.Status.NotRx,
    BindingContext = _interestingSystem,
};
broadcastCell.SetBinding(BroadcastCell.HeartbeatProperty, "HeartbeatStatus");
StatkStack.Children.Add(broadcastCell);

These bindings work, and I can see the code-behind responding to the model changes just fine.这些绑定有效,我可以看到响应 model 更改的代码隐藏很好。

Now I want to bind my view to the code-behind after a little modification and adjustment.现在我想在稍作修改和调整后将我的视图绑定到代码隐藏。
XAML XAML

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="56" />
        <RowDefinition Height="20" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="65" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Label Text="{Binding Title, FallbackValue=TestTitle, Mode=OneWay}" Grid.Column="1"  />
    <Label Text="{Binding SecondaryLine, FallbackValue=Test2nd}" Grid.Column="1" Grid.Row="1"  />
</Grid>

Code-behind代码隐藏

public Heartbeat.Status CurrentStatus;

static void OnHeartbeatStatusChanged(BindableObject sender, object oldValue, object newValue)
{
    var thisInstance = (BroadcastCell)sender;
    var newStatus = (Heartbeat.Status)newValue;
    thisInstance.CurrentStatus= newStatus;
}

private void UpdateAndModifyResult()
{
    SecondaryLine = $"{DateTime.Now} {CurrentStatus} @ {AnotherStatus}";
}

public String SecondaryLine
{
    get { return _secondaryLine; }
    set
    {
        _secondaryLine = value;
        OnPropertyChanged();
    }
}

Can I set the binding of the Xaml to the code-behind, and the code behind to another object as I am trying?我可以尝试将 Xaml 的绑定设置为代码隐藏,并将后面的代码设置为另一个 object 吗?
Or do I need to hard-code in the xaml property name into the code-behind to update them 'manually'?或者我是否需要将 xaml 属性名称硬编码到代码隐藏中以“手动”更新它们?

In XAML, you can set the source of a Binding to the root BroadcastCell by using the FindAncestor binding mode like this:在 XAML 中,您可以使用FindAncestor绑定模式将Binding的源设置为根BroadcastCell ,如下所示:

<Label Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:BroadcastCell}}, Path=SecondaryLine}" Grid.Column="1" Grid.Row="1"/>

When loading the XAML, the binding engine will look up the visual tree for the first BroadcastCell object and bind to its SecondaryLine property.加载 XAML 时,绑定引擎将查找第一个BroadcastCell object 的可视化树并绑定到其SecondaryLine属性。

Alternatively, you could directly bind your view to your model without the need to manually set any kind of binding in your code-behind by doing this:或者,您可以直接将您的视图绑定到您的 model 而无需通过执行以下操作在您的代码隐藏中手动设置任何类型的绑定:

<Label Text="{Binding Heartbeat.Status}" Grid.Column="1" Grid.Row="1"/>

provided that you set the DataContext of your view to the correct source object.前提是您将视图的DataContext设置为正确的源 object。 Your code-behind would become:您的代码隐藏将变为:

broadcastCell.DataContext = _interestingSystem;

This is more appropriate than defining a binding in the code-behind.这比在代码隐藏中定义绑定更合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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