WPF用户控件属性在新实例中不更改 [英] WPF User Control Properties Not Changing in New Instance
问题描述
下面是我的自定义控件的WPF XAML和VB,下面是我在新WPF XAML中的用法,但我发现除非我在用户控件VB中更改它们,否则我无法更改属性。 / p>
这是我第一次这样做,寻找一些积极的反馈和更好的方法来做到这一点
我也有绑定自定义控件的高度和宽度的问题这些似乎没有工作时间
谢谢
我没有在WPF VB中输入最终形式,因为此时它是空的
Madaxe
< Window
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d =" http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local =" clr-namespace:DataBinding"
xmlns:vsc =" clr-namespace:UserControl; assembly = ClockControl"的xmlns:用户控件= QUOT; CLR-名称空间:DataBinding.UserControl" X:类= QUOT;主窗口"
mc:Ignorable =" d"
Title =" MainWindow"高度= QUOT; 450"宽度= QUOT; 800"
>
< Grid>
< UserControl:ClockControl x:Name =" MyClock"
HorizontalAlignment =" Left"
Margin =" 210,119,0,0"
VerticalAlignment =" Top"
ClockBezelBorderThickness =" 20"
ClockBezelBrushColor =" Blue"
ClockBezelBackground =" Gray"
ClockBezelCornerRadius =" 360"
ClockHeight =" 300"
ClockWidth =" 300" />
< / Grid>
< / Window>
< UserControl x:Class =" UserControl.ClockControl"
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"
xmlns:local =" clr-namespace:DataBinding"
mc:Ignorable =" d"
DataContext =" {Binding RelativeSource = {RelativeSource Self}}"
d:DesignHeight =" {Binding Path = ClockHeight}"
d:DesignWidth =" {Binding Path = ClockWidth}"
Loaded =" Clock_Loaded"
背景="透明">
< Border CornerRadius =" {Binding Path = ClockBezelCornerRadius}"
BorderBrush =" {Binding Path = ClockBezelBrushColor}"
BorderThickness =" {Binding Path = ClockBezelBorderThickness}"
Background =" {Binding Path = ClockBezelBackground}">
< StackPanel WindowChrome.IsHitTestVisibleInChrome =" True">
< Label Foreground =" {Binding Path = ClockTitleFontForeGround}"
Margin =" 0,15,0,0"
FontSize =" {Binding Path = ClockTitleFontSize}"
FontFamily =" {Binding Path = ClockTitleFont}"
HorizontalAlignment =" Center"
Content =" {Binding Path = ClockTitle}" />
< StackPanel Background ="#3F3F46"余量= QUOT; 0,20,0,5"宽度= QUOT; 280"高度= QUOT; 100">
< Label Name =" LBL_Time"余量= QUOT; 0,20,0,0"前景= QUOT;#FFDF66"字号= QUOT; 40"的FontFamily = QUOT; Stormfaze"的HorizontalAlignment = QUOT;中心"→10:30:40℃; /标签>
< / StackPanel>
< StackPanel Background ="#3F3F46"宽度= QUOT; 280"高度= QUOT; 50">
< Label Name =" LBL_Date"余量= QUOT; 0,8,0,0"前景= QUOT;#ABA796"字号= QUOT; 20" FontFamily ="Britannic Bold" HorizontalAlignment =" Center"> Sat 01.05.2018< / Label>
< / StackPanel>
< / StackPanel>
< / Border>
< / UserControl>
Imports System.Windows.Threading
Namespace UserControl
Public Class ClockControl
Private WithEvents _Timer As DispatcherTimer = Nothing
Public Property ClockHeight = 300
Public Property ClockWidth = 300
公共财产ClockBezelCornerRadius As Integer = 20
公共财产ClockBezelBrushColor As String =" Green"
Public Property ClockBezelBackground As String =" Yellow"
Public Property ClockBezelBorderThickness As Integer = 5
Public Property ClockTitle As String =" DIGICLOCK"
Public Property ClockTitleFont As String =" Stormfaze"
公共属性ClockTitleFontSize As Integer = 25
公共属性ClockTitleFontForeGround As String =" Gray"
Sub New()
'设计师需要此调用。
InitializeComponent()
'在InitializeComponent()调用之后添加任何初始化。
End Sub
Private Sub Clock_Loaded(sender As Object,e As RoutedEventArgs)处理Me.Loaded
_Timer = New DispatcherTimer
_Timer.Interval = TimeSpan.FromSeconds( 1)
_Timer.Start()
End Sub
Private Sub Timer_Tick()处理_Timer.Tick
LBL_Time.Content = DateTime.Now.ToLongTimeString
LBL_Date.Content = DateTime.Now.ToLongDateString
End Sub
End Class
End Namespace
在贝鲁特忙碌作为一个砖头
您缺少INotifyPropertyChanged的实现。 当您对属性进行更改时,这种机制会告诉UI处理属性的更改。
以下是您需要的一个小示例:
Imports System.ComponentModel
Class MainWindow
Implements INotifyPropertyChanged
Private _sampleProperty As String
公共属性sampleProperty()As String
获取
返回_sampleProperty
结束获取
设置(ByVal值为字符串)
_sampleProperty = value
RaiseChanged(" sampleProperty")
End Set
End Property
Private Sub RaiseChanged(propName As String)
RaiseEvent PropertyChanged(Me,New PropertyChangedEventArgs(propName) )
End Sub
公共事件PropertyChanged As PropertyChangedEventHandler实现INotifyPropertyChanged.PropertyChanged
结束类
Below is the WPF XAML and VB for my custom control, also below is my usage in the new WPF XAML but I'm finding that i cant change the properties unless i change them in the user control VB.
This is the first time I've done this, looking for some positive feed back and better ways to do this
I'm also having issues binding the height and width of the custom control these don't seem to work period
Thanks
i didn't put in the WPF VB for the final form since its empty at this point
Madaxe
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:DataBinding" xmlns:vsc="clr-namespace:UserControl;assembly=ClockControl" xmlns:UserControl="clr-namespace:DataBinding.UserControl" x:Class="MainWindow" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800" > <Grid> <UserControl:ClockControl x:Name="MyClock" HorizontalAlignment="Left" Margin="210,119,0,0" VerticalAlignment="Top" ClockBezelBorderThickness="20" ClockBezelBrushColor="Blue" ClockBezelBackground="Grey" ClockBezelCornerRadius="360" ClockHeight="300" ClockWidth="300"/> </Grid> </Window>
<UserControl x:Class="UserControl.ClockControl" 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" xmlns:local="clr-namespace:DataBinding" mc:Ignorable="d" DataContext="{Binding RelativeSource={RelativeSource Self}}" d:DesignHeight="{Binding Path=ClockHeight}" d:DesignWidth="{Binding Path=ClockWidth}" Loaded="Clock_Loaded" Background="Transparent"> <Border CornerRadius="{Binding Path=ClockBezelCornerRadius}" BorderBrush="{Binding Path=ClockBezelBrushColor}" BorderThickness="{Binding Path=ClockBezelBorderThickness}" Background="{Binding Path=ClockBezelBackground}"> <StackPanel WindowChrome.IsHitTestVisibleInChrome="True"> <Label Foreground="{Binding Path=ClockTitleFontForeGround}" Margin="0,15,0,0" FontSize="{Binding Path=ClockTitleFontSize}" FontFamily="{Binding Path=ClockTitleFont}" HorizontalAlignment="Center" Content="{Binding Path=ClockTitle}"/> <StackPanel Background="#3F3F46" Margin="0,20,0,5" Width="280" Height="100"> <Label Name="LBL_Time" Margin="0,20,0,0" Foreground="#FFDF66" FontSize="40" FontFamily="Stormfaze" HorizontalAlignment="Center">10:30:40</Label> </StackPanel> <StackPanel Background="#3F3F46" Width="280" Height="50"> <Label Name="LBL_Date" Margin="0,8,0,0" Foreground="#ABA796" FontSize="20" FontFamily="Britannic Bold" HorizontalAlignment="Center">Sat 01.05.2018</Label> </StackPanel> </StackPanel> </Border> </UserControl>
Imports System.Windows.Threading Namespace UserControl Public Class ClockControl Private WithEvents _Timer As DispatcherTimer = Nothing Public Property ClockHeight = 300 Public Property ClockWidth = 300 Public Property ClockBezelCornerRadius As Integer = 20 Public Property ClockBezelBrushColor As String = "Green" Public Property ClockBezelBackground As String = "Yellow" Public Property ClockBezelBorderThickness As Integer = 5 Public Property ClockTitle As String = "DIGICLOCK" Public Property ClockTitleFont As String = "Stormfaze" Public Property ClockTitleFontSize As Integer = 25 Public Property ClockTitleFontForeGround As String = "Grey" Sub New() ' This call is required by the designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. End Sub Private Sub Clock_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded _Timer = New DispatcherTimer _Timer.Interval = TimeSpan.FromSeconds(1) _Timer.Start() End Sub Private Sub Timer_Tick() Handles _Timer.Tick LBL_Time.Content = DateTime.Now.ToLongTimeString LBL_Date.Content = DateTime.Now.ToLongDateString End Sub End Class End Namespace
As Busy As A Bricky In Beirut
You are missing an Implementation of INotifyPropertyChanged. When you make a change to a property it is this mechanism that tells the UI to deal with changes to properties.
The following is a small example of what you need:
Imports System.ComponentModel Class MainWindow Implements INotifyPropertyChanged Private _sampleProperty As String Public Property sampleProperty() As String Get Return _sampleProperty End Get Set(ByVal value As String) _sampleProperty = value RaiseChanged("sampleProperty") End Set End Property Private Sub RaiseChanged(propName As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName)) End Sub Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged End Class
这篇关于WPF用户控件属性在新实例中不更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!