如果我有这个:
public class BoardCalc : FrameworkElement
{
public BoardCalc()
{
this.Loaded += BoardCalc_Loaded;
}
void BoardCalc_Loaded(object sender, RoutedEventArgs e)
{
Boards = Math.Floor(LengthRequired / 16);
BoardsRequired2 = Math.Floor(LengthRequired / 16);
}
public Double LengthRequired { get; set; }
private Double _Boards;
public Double Boards
{
get
{
return _Boards;
}
set
{
_Boards = value;
}
}
//public Double Boards
//{
// get { return (Double)GetValue(BoardsProperty); }
// set { SetValue(BoardsProperty, value); }
//}
//// Using a DependencyProperty as the backing store for Boards. This enables animation, styling, binding, etc...
//public static readonly DependencyProperty BoardsProperty =
// DependencyProperty.Register("Boards", typeof(Double), typeof(BoardCalc), null);
public Double BoardsRequired2
{
get { return (Double)GetValue(BoardsRequired2Property); }
set { SetValue(BoardsRequired2Property, value); }
}
// Using a DependencyProperty as the backing store for BoardsRequired2. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BoardsRequired2Property =
DependencyProperty.Register("BoardsRequired2", typeof(Double), typeof(BoardCalc), null);
}
我这样做:
<StackPanel xmlns:Boards="clr-namespace:BoardsUtil" >
<Boards:BoardCalc x:Name="boardCalc1"
LengthRequired="5280" />
<TextBlock Text="{Binding ElementName=boardCalc1, Path=Boards}" />
<TextBlock Text="{Binding ElementName=boardCalc1, Path=BoardsRequired2}" />
</StackPanel>
两个问题:
当我使用依赖属性时,在设计时,Boards的值将被计算为330。如果我使用普通属性,则在设计时为0. 在运行时,任何一个都可以正常工作。这是否是我们所期望的?如果是或者不是,能否有人解释一下为什么,并指导我如何解决它并检查我的其余代码是否实际工作。
对于LengthRequired,我应该使用依赖属性吗?如果您从XAML设置属性,则应该使用依赖属性吗?但是,如果您仅从XAML绑定到属性,则可以使用普通属性?这是我们看到的行为吗?这是否是我们所期望的?为什么,让我决定做什么。