我在頁面上顯示時間/時鐘作爲用戶控件(ClockControl
),實際時間模型是從另一個類的DateTime
對象驅動的( ClockTime
)。我對ClockControl
2周的TextBlocks:關於用戶控件中DependencyProperty的setter中代碼的問題
文字塊「Second_update_by_binding」勢必又被綁定到模型ClockTime「二」依賴屬性「Second_binded」。
正文塊「Second_update_by_manipulating」由操縱模型ClockTime「第二」,使得它增加了的值來更新「0」,在前面,如果「第二」僅是1位(或小於10)
我設法實現我想要的,不管它是否是最好的方法。但是,我遇到了幾個我不太明白爲什麼會發生的問題。具體來說,它是時鐘用戶控件中依賴項屬性中getter/setter內部代碼的邏輯,這是我最困惑的。
MainPage.xaml中:
<Page x:Class="App1.MainPage" ...">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<local:ClockControl x:Name="useControl"
Second_binded="{Binding Second}"
Second_manipulated="{Binding Second}" />
</Grid>
</Page>
MainPage.cs:
public sealed partial class MainPage : Page
{
ClockTime clock;
DispatcherTimer Timer;
public MainPage()
{
clock = new ClockTime();
this.InitializeComponent();
this.DataContext = clock;
// I am adding this DispatchTimer to force the update of the text
// 'Second_update_by_manipulating' on the ClockControl since the
// Binding of Second_manipulated doesnt work
Timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 1) };
Timer.Tick += Timer_Tick;
Timer.Start();
}
private void Timer_Tick(object sender, object e)
{
useControl.Second_manipulated = clock.Second.ToString();
}
}
ClockTime型號:
class ClockTime : INotifyPropertyChanged
{
public ClockTime()
{
var Timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 1) };
Timer.Tick += Timer_Tick;
Timer.Start();
}
private void Timer_Tick(object sender, object e)
{
Second = DateTime.Now.Second;
}
//===================================================
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
//===================================================
private int _second;
public int Second
{
get { return this._second; }
set
{
if (value != this._second)
{
this._second = value;
NotifyPropertyChanged("Second");
}
}
}
}
ClockControl.xaml:
<UserControl x:Class="App1.ClockControl" ...>
<Grid>
<StackPanel>
<TextBlock x:Name="Second_update_by_manipulating" />
<TextBlock x:Name="Second_update_by_binding" Text="{Binding Second_binded}" />
</StackPanel>
</Grid>
</UserControl>
ClockControl.cs:
public sealed partial class ClockControl : UserControl
{
public ClockControl()
{
this.InitializeComponent();
(this.Content as FrameworkElement).DataContext = this;
}
//==================================
public String Second_binded
{
get {
return (String)GetValue(Second_bindedProperty);
}
set {
Debug.WriteLine(" in Second_binded ");
SetValue(Second_bindedProperty, value);
}
}
public static readonly DependencyProperty Second_bindedProperty =
DependencyProperty.Register(
"Second_binded",
typeof(string),
typeof(ClockControl),
new PropertyMetadata(""));
//==================================
public String Second_manipulated
{
get
{
return (String)GetValue(Second_manipulatedProperty);
}
set
{
Second_update_by_manipulating.Text = (Convert.ToInt32(value)<10) ? "0"+value : value;
Debug.WriteLine(" in Second_manipulated ");
SetValue(Second_manipulatedProperty, value);
}
}
public static readonly DependencyProperty Second_manipulatedProperty =
DependencyProperty.Register(
"Second_manipulated",
typeof(string),
typeof(ClockControl),
new PropertyMetadata(""));
//==================================
}
所以這裏是我的問題:
爲什麼調試代碼的Debug.WriteLine( 「在Second_binded」);在模型ClockTime中的'Second'正在更新時,ClockControl中的Second_binded依賴項屬性的setter中的永遠不會被調用。
代碼Debug.WriteLine(「in Second_manipulated」); ClockControl中的Second_manipulated依賴項屬性的setter被調用且代碼Value_1.Text =(Convert.ToInt32(value)< 10)? 「0」+ value:value;執行以更改ClockControl上的文本,但僅在添加另一個DispatchTimer並使用MainPage.cs強制執行代碼後才起作用useControl.Second_manipulated = clock.Second.ToString();更新時間。爲什麼我必須這樣做來更新Second_manipulated,即使我已經在MainPage.xaml中將Second_manipulated綁定爲Second了?
任何想法和意見,啓發我的知識在C#非常受歡迎。
感謝
肯
@marc_s,謝謝你更新我的文本:-) –