数据绑定通过用户控件

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

Data binding through UserControl

问题

这是一个数据绑定的最小示例。我想知道如何将ToggleSwitchButton分别移到不同的UserControl中,同时保持数据绑定。

//MainWindow.xaml
<StackPanel>
    <ToggleSwitch x:Name="MyToggle" OffContent="Off" OnContent="On"/>
    <Button IsEnabled="{x:Bind MyToggle.IsOn, Mode=OneWay}" Content="Click Me"/>
</StackPanel>

// MainWindow.idl
Microsoft.UI.Xaml.Controls.ToggleSwitch MyToggle{ get; };
英文:

Here's a minimal example of data binding. I would like know how to factor out both the ToggleSwitch and Button into different UserControls while keeping the data binding.

//MainWindow.xaml
<StackPanel>
    <ToggleSwitch x:Name="MyToggle" OffContent="Off" OnContent="On"/>
    <Button IsEnabled="{x:Bind MyToggle.IsOn, Mode=OneWay}" Content="Click Me"/>
</StackPanel>

// MainWindow.idl
Microsoft.UI.Xaml.Controls.ToggleSwitch MyToggle{ get; };

答案1

得分: 0

MyToggleControlMyToggle公开为公共属性,以便"{x:Bind MyToggleControl.MyToggle.IsOn}" 可用。然后创建IsButtonEnabled属性,以便IsButtonEnabled="{x:Bind ...}" 将正常工作。


UserControlToggle公开为MainWindow的公共属性

// MainWindow.xaml
<StackPanel>
    <local:UserControlToggle x:Name="MyToggleControl" x:FieldModifier="public"/>
    <local:UserControlButton IsButtonEnabled="{x:Bind MyToggleControl.MyToggle.IsOn, Mode=OneWay}"/>
</StackPanel>

// MainWindow.idl,用于 cppwinrt,因为 x:FieldModifier 不起作用
import "UserControlToggle.idl";
UserControlToggle MyToggleControl{ get; };

ToggleSwitch公开为UserControlToggle的公共属性

// UserControlToggle.xaml
<ToggleSwitch x:Name="MyToggle" x:FieldModifier="public" OffContent="Off" OnContent="On"/>

// UserControlToggle.idl
Microsoft.UI.Xaml.Controls.ToggleSwitch MyToggle{ get; };

将按钮的IsEnabled属性包装到UserControlButton的属性中

// UserControlButton.xaml
<Button x:Name="myButton" Click="myButton_Click">启用/禁用</Button>

// UserControlButton.idl
Boolean IsButtonEnabled;

// UserControlButton.xaml.cpp
bool UserControl::IsButtonEnabled()
{
    return myButton().IsEnabled();
}

void UserControl::IsButtonEnabled(bool value)
{
    myButton().IsEnabled(value);
}
英文:

Publically expose MyToggleControl and MyToggle so "{x:Bind MyToggleControl.MyToggle.IsOn}" is available. Then create IsButtonEnabled property so that IsButtonEnabled="{x:Bind ...}" will work.


Expose the UserControlToggle as a public property of MainWindow

// MainWindow.xaml
<StackPanel>
    <local:UserControlToggle x:Name="MyToggleControl" x:FieldModifier="public"/>
    <local:UserControlButton IsButtonEnabled="{x:Bind MyToggleControl.MyToggle.IsOn, Mode=OneWay}"/>
</StackPanel>

// MainWindow.idl needed for cppwinrt as x:FieldModifier doesn't work
import "UserControlToggle.idl";
UserControlToggle MyToggleControl{ get; };

Expose the ToggleSwitch as a public property of UserControlToggle

// UserControlToggle.xaml
<ToggleSwitch x:Name="MyToggle" x:FieldModifier="public" OffContent="Off" OnContent="On"/>

// UserControlToggle.idl
Microsoft.UI.Xaml.Controls.ToggleSwitch MyToggle{ get; };

Wrap the buttons IsEnabled property to a property on UserControlButton

// UserControlButton.xaml
<Button x:Name="myButton" Click="myButton_Click">Enable/Disable</Button>

// UserControlButton.idl
Boolean IsButtonEnabled;

// UserControlButton.xaml.cpp
bool UserControl::IsButtonEnabled()
{
    return myButton().IsEnabled();
}

void UserControl::IsButtonEnabled(bool value)
{
    myButton().IsEnabled(value);
}

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

发表评论

匿名网友

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

确定