英文:
Data binding through UserControl
问题
这是一个数据绑定的最小示例。我想知道如何将ToggleSwitch
和Button
分别移到不同的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 UserControl
s 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
将MyToggleControl
和MyToggle
公开为公共属性,以便"{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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论