【发布时间】:2014-08-01 19:00:42
【问题描述】:
在下面的代码中,我有一个包含椭圆和文本块的 UserControl。我想创建一个可以绑定的可重用控件,它允许我根据字符串设置文本,并根据布尔值更改红色/绿色之间的填充颜色。
我现在可以通过深入研究标记并使用一些复杂的绑定来做到这一点,但是我想在列表中重用这个控件,并且为此目的创建一个控件似乎更容易。但是,我不确定下一步该去哪里,是否应该创建与 Fill 和 Text 的值相关的依赖属性,或者什么。
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Herp.Derp.View.DeliveryStatusIndicator"
x:Name="UserControl"
d:DesignWidth="91" d:DesignHeight="35">
<Grid x:Name="LayoutRoot">
<StackPanel Orientation="Horizontal">
<Ellipse Width="35" Height="35" Fill="Green">
<Ellipse.OpacityMask>
<VisualBrush Stretch="Fill" Visual="{StaticResource appbar_location_circle}"/>
</Ellipse.OpacityMask>
</Ellipse>
<TextBlock Style="{StaticResource Heading2}"
VerticalAlignment="Center" Margin="3,0,0,0">
<Run Text="FRONT"/>
</TextBlock>
</StackPanel>
</Grid>
</UserControl>
【问题讨论】:
-
是的,在用户控件中创建依赖属性并从外部设置它们。在用户控件内部将控件绑定到自己的依赖属性
标签: c# wpf xaml controls dependency-properties