Я помещаю пользовательский контроль внутри пользовательского управления, и у меня возникают проблемы с привязкой внутренней зависимости свойства с свойством внешней зависимости. Я использую .net 8. Это, вероятно, что -то глупое, что мне не хватает, но я просто не вижу его. Во внешнем пользовательском управлении у меня есть свойство зависимости, называемое Hourval. Во внешнем пользовательском управлении XAML я стараюсь связать свойство внутреннего управления CurrentVal с свойством внешнего управления, но когда изменяется свойство внутреннего управления, оно не распространяется на свойство внешнего управления. < /P>
Код следует.
< /code>
Внутренний код позади: < /p>
namespace WpfUserControls.UserControls
{
///
/// Interaction logic for UcIntegerPicker.xaml
///
public partial class UcIntegerPicker : UserControl
{
#region Constructor
public UcIntegerPicker()
{
InitializeComponent();
}
#endregion Constructor
#region Dependancy Properties
public int LowerLimit
{
get { return (int)GetValue(LowerLimitProperty); }
set { SetValue(LowerLimitProperty, value); }
}
// Using a DependencyProperty as the backing store for LowerLimit. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LowerLimitProperty =
DependencyProperty.Register("LowerLimit", typeof(int), typeof(UcIntegerPicker), new PropertyMetadata(0));
public int UpperLimit
{
get { return (int)GetValue(UpperLimitProperty); }
set { SetValue(UpperLimitProperty, value); }
}
// Using a DependencyProperty as the backing store for UpperLimit. This enables animation, styling, binding, etc...
public static readonly DependencyProperty UpperLimitProperty =
DependencyProperty.Register("UpperLimit", typeof(int), typeof(UcIntegerPicker), new PropertyMetadata(0));
public int CurrentVal
{
get { return (int)GetValue(CurrentValProperty); }
set
{
SetValue(CurrentValProperty, value);
string strVal = value.ToString();
if (strVal != CurrentStr)
{
CurrentStr = strVal;
}
}
}
// Using a DependencyProperty as the backing store for CurrentVal. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CurrentValProperty =
DependencyProperty.Register("CurrentVal", typeof(int), typeof(UcIntegerPicker), new PropertyMetadata(0));
public string CurrentStr
{
get { return (string)GetValue(CurrentStrProperty); }
set
{
SetValue(CurrentStrProperty, value);
Int32 intVal = Convert.ToInt32(value);
if (intVal != CurrentVal)
{
CurrentVal = intVal;
}
}
}
// Using a DependencyProperty as the backing store for CurrentStr. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CurrentStrProperty =
DependencyProperty.Register("CurrentStr", typeof(string), typeof(UcIntegerPicker), new PropertyMetadata("0"));
public string ButtonLabel
{
get { return (string)GetValue(ButtonLabelProperty); }
set { SetValue(ButtonLabelProperty, value); }
}
// Using a DependencyProperty as the backing store for ButtonLabel. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ButtonLabelProperty =
DependencyProperty.Register("ButtonLabel", typeof(string), typeof(UcIntegerPicker), new PropertyMetadata(" H "));
public int TextBlockWidth
{
get { return (int)GetValue(TextBlockWidthProperty); }
set { SetValue(TextBlockWidthProperty, value); }
}
// Using a DependencyProperty as the backing store for TextBlockWidth. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextBlockWidthProperty =
DependencyProperty.Register("TextBlockWidth", typeof(int), typeof(UcIntegerPicker), new PropertyMetadata(0));
public int TextBlockHeight
{
get { return (int)GetValue(TextBlockHeightProperty); }
set { SetValue(TextBlockHeightProperty, value); }
}
// Using a DependencyProperty as the backing store for TextBlockHeight. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextBlockHeightProperty =
DependencyProperty.Register("TextBlockHeight", typeof(int), typeof(UcIntegerPicker), new PropertyMetadata(0));
public int TextBlockFontSize
{
get { return (int)GetValue(TextBlockFontSizeProperty); }
set { SetValue(TextBlockFontSizeProperty, value); }
}
// Using a DependencyProperty as the backing store for TextBlockFontSize. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextBlockFontSizeProperty =
DependencyProperty.Register("TextBlockFontSize", typeof(int), typeof(UcIntegerPicker), new PropertyMetadata(0));
public int SliderWidth
{
get { return (int)GetValue(SliderWidthProperty); }
set { SetValue(SliderWidthProperty, value); }
}
// Using a DependencyProperty as the backing store for SliderWidth. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SliderWidthProperty =
DependencyProperty.Register("SliderWidth", typeof(int), typeof(UcIntegerPicker), new PropertyMetadata(0));
#endregion Dependancy Properties
#region Private Methods
private void ButtonChange_Click(object sender, RoutedEventArgs e)
{
popSlider.IsOpen = true;
sliderSetting.Focus();
}
private void SliderSetting_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
{
CurrentVal = ((Int32)e.NewValue);
}
private void SliderSetting_LostFocus(object sender, RoutedEventArgs e)
{
popSlider.IsOpen = false;
}
#endregion Private Methods
}
}
< /code>
Внешнее пользовательское управление xaml: < /p>
< /code>
Внешний код позади: < /p>
namespace WpfUserControls.UserControls
{
///
/// Interaction logic for UcTimePicker24.xaml
///
public partial class UcTimePicker24 : UserControl
{
#region Constructor
public UcTimePicker24()
{
InitializeComponent();
TimeVal = new TimeOnly(7, 8, 9, 5);
}
#endregion Constructor
#region Dependancy Properties
public int TextBlockHeight
{
get { return (int)GetValue(TextBlockHeightProperty); }
set { SetValue(TextBlockHeightProperty, value); }
}
// Using a DependencyProperty as the backing store for TextBlockHeight. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextBlockHeightProperty =
DependencyProperty.Register("TextBlockHeight", typeof(int), typeof(UcTimePicker24), new PropertyMetadata(10));
public int TextBlockWidth
{
get { return (int)GetValue(TextBlockWidthProperty); }
set { SetValue(TextBlockWidthProperty, value); }
}
// Using a DependencyProperty as the backing store for TextBlockWidth. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextBlockWidthProperty =
DependencyProperty.Register("TextBlockWidth", typeof(int), typeof(UcTimePicker24), new PropertyMetadata(10));
public int TextBlockFontSize
{
get { return (int)GetValue(TextBlockFontSizeProperty); }
set { SetValue(TextBlockFontSizeProperty, value); }
}
// Using a DependencyProperty as the backing store for TextBlockFontSize. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextBlockFontSizeProperty =
DependencyProperty.Register("TextBlockFontSize", typeof(int), typeof(UcTimePicker24), new PropertyMetadata(8));
public TimeOnly TimeVal
{
get { return (TimeOnly)GetValue(TimeValProperty); }
set
{
SetValue(TimeValProperty, value);
Int32 hour = value.Hour;
if (hour != HourVal)
{
HourVal = hour;
}
}
}
// Using a DependencyProperty as the backing store for TimeVal. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TimeValProperty =
DependencyProperty.Register("TimeVal", typeof(TimeOnly), typeof(UcTimePicker24), new PropertyMetadata(TimeOnly.MinValue));
public Int32 HourVal
{
get { return (Int32)GetValue(HourValProperty); }
set
{
SetValue(HourValProperty, value);
if (TimeVal.Hour != value)
{
TimeVal = new TimeOnly(value, TimeVal.Minute, TimeVal.Second, TimeVal.Millisecond);
}
string strVal = value.ToString();
if (strVal != HourStr)
{
HourStr = strVal;
}
}
}
// Using a DependencyProperty as the backing store for HourVal. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HourValProperty =
DependencyProperty.Register("HourVal", typeof(Int32), typeof(UcTimePicker24), new PropertyMetadata(0));
public string HourStr
{
get { return (string)GetValue(HourStrProperty); }
set
{
SetValue(HourStrProperty, value);
Int32 intVal = Convert.ToInt32(value);
if (intVal != HourVal)
{
HourVal = intVal;
}
}
}
// Using a DependencyProperty as the backing store for HourStr. This enables animation, styling, binding, etc...
public static readonly DependencyProperty HourStrProperty =
DependencyProperty.Register("HourStr", typeof(string), typeof(UcTimePicker24), new PropertyMetadata("0"));
#endregion Dependancy Properties
#region Private Methods
#endregion Private Methods
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... l-property
Проблема привязка внутреннего свойства управления пользователем с внешним свойством управления пользователем [Duplicate] ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение