[英]Bind data from ViewModel to elements of a control template
我正在尝试通过在app.xaml
文件中app.xaml
自定义样式来创建自定义日历样式,并将其绑定到mainwindow.xaml
的日历,此主窗口附加到ViewModel
,其中包含用于整个应用程序 ui 设计的画笔值。 一切正常,除非尝试将画笔 ex {Binding AccentRestBrush}
的值绑定到CalendarDayButton
的组件样式,如CalendarDayButton
.. 因为它不应用绑定到它的数据的值。 这是代码:
class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void UpdateProperty(string name)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public string BackgroundBrush { get { return new Colors().BackgroundBrush(); } }
public string CardBrush { get { return new Colors().CardBrush(); } }
public string ElementRestBrush { get { return new Colors().ElementRestBrush(); } }
public string ElementHoverBrush { get { return new Colors().ElementHoverBrush(); } }
public string AccentRestBrush { get { return new Colors().AccentRestBrush(); } }
public string AccentHoverBrush { get { return new Colors().AccentHoverBrush(); } }
public string ForegroundBrush { get { return new Colors().ForegroundBrush(); } }
public string ShadowBrush { get { return new Colors().ShadowBrush(); } }
public Window Window
{
set
{
if (value != null)
{
Properties.Settings.Default.PropertyChanged += (sender, e) =>
{
Properties.Settings.Default.Save();
UpdateProperty(nameof(BackgroundBrush));
UpdateProperty(nameof(CardBrush));
UpdateProperty(nameof(ElementRestBrush));
UpdateProperty(nameof(ElementHoverBrush));
UpdateProperty(nameof(AccentRestBrush));
UpdateProperty(nameof(AccentHoverBrush));
UpdateProperty(nameof(ForegroundBrush));
UpdateProperty(nameof(ShadowBrush));
};
}
}
}
}
ViewModel WindowViewModel = new()
{
Window = this,
};
this.DataContext = WindowViewModel;
它在这里不起作用
<Style x:Key="CalendarDayButtonStyle" TargetType="CalendarDayButton">
<Setter Property="Width" Value="30"/>
<Setter Property="Height" Value="30"/>
<Setter Property="FontSize" Value="15"/>
**<Setter Property="Background" Value="{Binding AccentRestBrush}"/>**
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
但在这里工作正常
<Style x:Key="AlphaCalendar" TargetType="Calendar">
<Setter Property="CalendarButtonStyle" Value="{DynamicResource CalendarButtonStyle}"/>
<Setter Property="CalendarDayButtonStyle" Value="{DynamicResource CalendarDayButtonStyle}"/>
<Setter Property="CalendarItemStyle" Value="{DynamicResource CalendarItemStyle}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Foreground" Value="#FF333333"/>
<Setter Property="Background" Value="{Binding BackgroundBrush}"/>
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="4" Direction="0" Opacity=".5" ShadowDepth="0" Color="Black"/>
</Setter.Value>
</Setter>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.