我正在尝试了解内容控件的工作方式以及如何将其放置在我正在构建的用户控件中。根据我的理解,我的用户控件应该有一个对象依赖属性,然后我应该能够将内容控件添加到我的用户控件并将其绑定到依赖属性。
我尝试过用户控件XAML
的各种变体,包括绕过ScrollViewer
元素并将ContentControl
置于用户控件XAML
的基础网格中。< / p>
如何将此图像放在此控件中?更广泛地说,如何在用户控件中放置任何XAML
?
用户控制CS
namespace CoreProject.UserControls
{
public sealed partial class ZoomControl : UserControl
{
public ZoomControl()
{
this.InitializeComponent();
}
#region Properties
public static readonly DependencyProperty ZoomContentProperty =
DependencyProperty.Register("ZoomContent", typeof(object), typeof(ZoomControl), new PropertyMetadata(null));
public object ZoomContent
{
get { return (object)GetValue(ZoomContentProperty); }
set { SetValue(ZoomContentProperty, value); }
}
#endregion
public void UnZoom()
{
// unzoom
ImageScrollViewer.Visibility = Visibility.Visible;
var period = TimeSpan.FromMilliseconds(10);
Windows.System.Threading.ThreadPoolTimer.CreateTimer(async (source) =>
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
ImageScrollViewer.ChangeView(0.0, 0.0, 0.8F, true);
ImageScrollViewer.Visibility = Visibility.Collapsed;
});
}, period);
}
public void ZoomToPosition(double zoomOriginX, double zoomOriginY, float zoomFactor, bool disableAnimations)
{
ImageScrollViewer.Visibility = Visibility.Visible;
var period = TimeSpan.FromMilliseconds(10);
Windows.System.Threading.ThreadPoolTimer.CreateTimer(async (source) =>
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
ImageScrollViewer.ChangeView(zoomOriginX, zoomOriginY, 1.2F, disableAnimations);
});
}, period);
}
}
}
用户控制Xaml
<Grid>
<ScrollViewer x:Name="ImageScrollViewer"
ZoomMode="Enabled"
Visibility="Collapsed"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Background="Transparent"
HorizontalScrollMode="Enabled"
VerticalScrollMode="Enabled"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden">
<Grid>
<!-- HERE IS WHERE I WANT TO PLACE MY CONTENT -->
<ContentControl Content="{Binding ZoomContent, ElementName=zoomContent}" />
</Grid>
</ScrollViewer>
</Grid>
用户控制用法
<usercontrols:ZoomControl x:Name="ZoomControl">
<usercontrols:ZoomControl.ZoomContent>
<!-- THIS IS HOW I WANT TO ADD MY CONTENT, SIMPLY PLACE XAML ELEMENT -->
<Image Source="/Assets/colocationDataCenterData.jpg" />
</usercontrols:ZoomControl.ZoomContent>
</usercontrols:ZoomControl>
答案 0 :(得分:2)
此绑定意味着“找到名为zoomContent的UI元素,并绑定到该对象的ZoomContent
属性”。它没有用,因为范围内没有zoomContent
这样的元素。
<ContentControl Content="{Binding ZoomContent, ElementName=zoomContent}" />
ZoomContent
是UserControl的属性。因此,我建议您在UserControl
的根级找到ZoomContent.xaml
标记,并为其命名 - 例如userControl
:
<UserControl
...
x:Name="userControl"
...
并绑定到那个:
<ContentControl Content="{Binding ZoomContent, ElementName=userControl}" />
然而,@ JohnnyWestlake比我更了解UWP。使用他的解决方案,在这种情况下更正确。
答案 1 :(得分:2)
<Grid>
<!-- HERE IS WHERE I WANT TO PLACE MY CONTENT -->
<ContentControl Content="{x:Bind ZoomContent, Mode=OneWay}" />
</Grid>
您也可以使用x:Bind - 它将Binding上下文作为类本身(您的UserControl),因此您可以直接绑定。
如果您希望经典绑定工作,您需要在usercontrol本身中命名和userControl。 (顺便说一下,通常你会把它作为TEMPLATED控件而不是使用模板绑定)
<UserControl x:Name="zoomContent">
<Grid>
<ScrollViewer x:Name="ImageScrollViewer"
ZoomMode="Enabled"
Visibility="Collapsed"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Background="Transparent"
HorizontalScrollMode="Enabled"
VerticalScrollMode="Enabled"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden">
<Grid>
<!-- HERE IS WHERE I WANT TO PLACE MY CONTENT -->
<ContentControl Content="{Binding ZoomContent, ElementName=zoomContent}" />
</Grid>
</ScrollViewer>
</Grid>
</UserControl>