如何從其他項目更改wpf用戶內容。例如,我有兩個用戶控制項目。當第二個wpf用戶項目中的時鐘打勾時,我想在第一個用戶控制項目中更改文本框。可能嗎 ?如何從其他項目更改wpf用戶內容
第一個項目
<UserControl x:Class="StatusBar.StatusBarWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="800">
<Grid>
<StatusBar VerticalAlignment="Bottom" Grid.Column="0" Background="{x:Null}">
<StatusBarItem HorizontalAlignment="Right">
<TextBox x:Name="TxtClock" IsReadOnly="True" Background="{x:Null}" BorderThickness="0" Foreground="Black" x:FieldModifier="public" Text="i want change" />
</StatusBarItem>
</StatusBar>
</Grid>
第二個項目
DispatcherTimer clock = new DispatcherTimer();
TxtSaat.Text = string.Format("{0:HH:mm:ss}", DateTime.Now);
clock.Tick += new EventHandler(Clock_Tick);
clock.Interval = new TimeSpan(0, 0, 1);
clock.Start();
private void Clock_Tick(object sender, EventArgs e)
{
TxtClock.Text = string.Format("{0:HH:mm:ss}", DateTime.Now);
}
我做到了。但用戶控制不更新。 –
讓我猜。你在窗口中有一個用戶控件,並且你使用'... = new MyUserControl();'創建了一個實例,你運行更新並且現在顯示沒有更新? –
是的。你是對的。 –