OnPropertyChanged 事件不会切换按钮可见性
OnPropertyChanged event doesn't toggle Button Visibility
我有一个按钮,只有当它上面的给定文本有效时才应该激活 URL,我得到了正确的正则表达式和一个 OnPropertyChanged 方法,在该方法中我将按钮可见性设置为 true(它在 xaml 文件中转换为可见性)...
虽然我将按钮可见性设置为 true 没有任何变化
视图模型代码:
private bool m_isSaveButtonVisible = true;
public bool IsSaveButtonVisible
{
get => m_isSaveButtonVisible;
set
{
m_isSaveButtonVisible = value;
OnPropertyChanged("???"); //i don't know exactly what to call here?
}
}
...
public event PropertyChangedEventHandler PropertyChanged;
protected override void OnPropertyChanged(PropertyChangedEventArgs args)
{
if (MeetingRole == WebRTCMeetingRole.Join)
{
if (Url != m_currentUrl)
{
m_currentUrl = Url;
if (Regex.Match(m_currentUrl, URL_PATTERN, RegexOptions.IgnoreCase).Success)
{
PropertyChanged.Invoke(this, e: args); //should set true
}
else
{
PropertyChanged.Invoke(this, e: args); //should set false
}
}
}
}
XAML代码:
<TextBlock Text="{x:Static p:Resources.webrtc_url}" Foreground="White" FontSize="18" Margin="0 0 0 10"/>
<c:WatermarkTextBox attached:FocusExtension.IsFocused="{Binding IsUrlFocused}"
Foreground="White" FontSize="19" WatermarkForeground="{x:Static co:Colors.Trout}"
Margin="0 0 0 30" Text="{Binding Url, Mode=TwoWay}"
Watermark="{x:Static p:Resources.webrtc_url_hint}" WatermarkHorizontalAlignment="Left" HasFocus="True" SelectAll="True"
EnterCommand="{Binding SaveCommand, Mode=OneTime}" />
...
<c:IconButton Text="{Binding ConfirmButtonText, Mode=OneWay}" TextAlignment="Center" Foreground="White" FontSize="16"
Background="{x:Static co:Colors.DarkOrange}" Margin="0 0 0 8"
Command="{Binding SaveCommand, Mode=OneTime}"
Visibility="{Binding IsSaveButtonVisible, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}"/>
有人知道为什么没有设置按钮可见性吗?
应该发生的是,当有人在文本字段中输入有效的 URL 时,保存按钮应该出现
通过 OnPropertyChange 当有人在文本字段中写东西时我已经注意到问题是我无法将按钮切换出此功能因为它没有设置可见性而且我不知道为什么
属性 更改只是通知 WPF 属性 已更改。仅此而已。
所以:
public event PropertyChangedEventHandler PropertyChanged;
private bool m_isSaveButtonVisible = true;
public bool IsSaveButtonVisible
{
get => m_isSaveButtonVisible;
set
{
m_isSaveButtonVisible = value;
// if somebody listens to PropertyChanged we tell him IsSaveButtonVisible has changed
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsSaveButtonVisible)));
}
}
应该够了。
我有一个按钮,只有当它上面的给定文本有效时才应该激活 URL,我得到了正确的正则表达式和一个 OnPropertyChanged 方法,在该方法中我将按钮可见性设置为 true(它在 xaml 文件中转换为可见性)...
虽然我将按钮可见性设置为 true 没有任何变化
视图模型代码:
private bool m_isSaveButtonVisible = true;
public bool IsSaveButtonVisible
{
get => m_isSaveButtonVisible;
set
{
m_isSaveButtonVisible = value;
OnPropertyChanged("???"); //i don't know exactly what to call here?
}
}
...
public event PropertyChangedEventHandler PropertyChanged;
protected override void OnPropertyChanged(PropertyChangedEventArgs args)
{
if (MeetingRole == WebRTCMeetingRole.Join)
{
if (Url != m_currentUrl)
{
m_currentUrl = Url;
if (Regex.Match(m_currentUrl, URL_PATTERN, RegexOptions.IgnoreCase).Success)
{
PropertyChanged.Invoke(this, e: args); //should set true
}
else
{
PropertyChanged.Invoke(this, e: args); //should set false
}
}
}
}
XAML代码:
<TextBlock Text="{x:Static p:Resources.webrtc_url}" Foreground="White" FontSize="18" Margin="0 0 0 10"/>
<c:WatermarkTextBox attached:FocusExtension.IsFocused="{Binding IsUrlFocused}"
Foreground="White" FontSize="19" WatermarkForeground="{x:Static co:Colors.Trout}"
Margin="0 0 0 30" Text="{Binding Url, Mode=TwoWay}"
Watermark="{x:Static p:Resources.webrtc_url_hint}" WatermarkHorizontalAlignment="Left" HasFocus="True" SelectAll="True"
EnterCommand="{Binding SaveCommand, Mode=OneTime}" />
...
<c:IconButton Text="{Binding ConfirmButtonText, Mode=OneWay}" TextAlignment="Center" Foreground="White" FontSize="16"
Background="{x:Static co:Colors.DarkOrange}" Margin="0 0 0 8"
Command="{Binding SaveCommand, Mode=OneTime}"
Visibility="{Binding IsSaveButtonVisible, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}"/>
有人知道为什么没有设置按钮可见性吗?
应该发生的是,当有人在文本字段中输入有效的 URL 时,保存按钮应该出现
通过 OnPropertyChange 当有人在文本字段中写东西时我已经注意到问题是我无法将按钮切换出此功能因为它没有设置可见性而且我不知道为什么
属性 更改只是通知 WPF 属性 已更改。仅此而已。
所以:
public event PropertyChangedEventHandler PropertyChanged;
private bool m_isSaveButtonVisible = true;
public bool IsSaveButtonVisible
{
get => m_isSaveButtonVisible;
set
{
m_isSaveButtonVisible = value;
// if somebody listens to PropertyChanged we tell him IsSaveButtonVisible has changed
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsSaveButtonVisible)));
}
}
应该够了。