gpt4 book ai didi

c# - 如何将用户控件属性绑定(bind)到其他属性?

转载 作者:太空宇宙 更新时间:2023-11-03 13:06:44 25 4
gpt4 key购买 nike

我有以下用户控件:

Xaml:

<UserControl x:Class="ScreenRecorder.TimePicker"
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" Height="27" Width="176">
<Grid>
<StackPanel Orientation="Horizontal">
<TextBox Width="150" Height="25" Text="{Binding Time}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<StackPanel Orientation="Vertical">
<Button Width="25" Height="12.5" HorizontalAlignment="Left" VerticalAlignment="Top" Click="btnKeyUp_Clicked">
<Image Source="up.png" Height="10" Width="10" VerticalAlignment="Top"/>
</Button>
<Button Width="25" Height="12.5" HorizontalAlignment="Left" VerticalAlignment="Top" Click="btnKeyDown_Clicked">
<Image Source="down.png" Height="10" Width="10" VerticalAlignment="Top"/>
</Button>
</StackPanel>
</StackPanel>
</Grid>

代码:

 public partial class TimePicker : UserControl, INotifyPropertyChanged
{
public TimePicker()
{
InitializeComponent();
this.DataContext = this;
//Time = m_time;
}

public static DependencyProperty TimeProperty = DependencyProperty.Register(
"Time", typeof(string), typeof(TimePicker));

//private string m_time = DateTime.Now.ToString();
public string Time
{
get { return (string)GetValue(TimeProperty); }
set
{
SetValue(TimeProperty, value);
NotifyPropertyChanged("Time");
}
}

private void btnKeyUp_Clicked(object sender, RoutedEventArgs e)
{
DateTime curTime = Convert.ToDateTime(Time);
curTime += new TimeSpan(0, 0, 1);
Time = curTime.ToString();
}

private void btnKeyDown_Clicked(object sender, RoutedEventArgs e)
{
DateTime curTime = Convert.ToDateTime(Time);
curTime -= new TimeSpan(0, 0, 1);
Time = curTime.ToString();
}

#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}

我还有另一个使用此用户控件的用户控件,如下所示:

        <StackPanel>
<Label Content="Begin Record Time" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5"/>
<local:TimePicker Time="{Binding StartRecordTime}"/>
</StackPanel>

StartRecordTime 看起来像这样:

public string StartRecordTime
{
get { return m_startRecord; }
set
{
m_startRecord = value;
NotifyPropertyChanged("StartRecordTime");
}
}

我想根据 Time 属性更改 StartRecordTime,反之亦然,但只有 Time 属性发生了变化。

谢谢。

最佳答案

试试这个:

<local:TimePicker Time="{Binding Path=StartRecordTime, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

关于c# - 如何将用户控件属性绑定(bind)到其他属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30560006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com