我有一個按鈕,只有當它上面的給定文本是一個有效的URL時,它才會被激活,我得到了正確的重碼和一個OnPropertyChanged方法,其中我將按鈕的可見性設定為true(它在xaml檔案中被轉換為可見性)。
盡管我把按鈕的可見性設定為真,但沒有任何變化
。ViewModel代碼:
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); //應設定為true。
}
else /應該設定true }
{
PropertyChanged.Invoke(this, e: args); //should set false {
}
}
}
}
XAML代碼:
<TextBlock Text="{x:static p:Resources. webrtc_url}" Foreground="白色" 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}"
水印="{x:靜態 p:資源。 webrtc_url_hint}" WatermarkHorizontalAlignment="Left"/span> HasFocus="True" SelectAll="True"
EnterCommand="{Binding SaveCommand, Mode=OneTime}"/span> />
...
<c: IconButton Text="{Binding ConfirmButtonText, Mode=OneWay}" TextAlignment="Center"/span> 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}"/span>/>
有沒有人知道為什么按鈕的可見性沒有被設定?
應該發生的是,當有人在Textfield中寫下一個有效的URL時,savebutton應該出現
。通過OnPropertyChange我已經注意到了,當有人在文本欄位中寫了一些東西時,問題是我不能從這個函式中切換按鈕,因為它沒有設定可見性,我不知道為什么
。uj5u.com熱心網友回復:
屬性改變只是通知WPF一個屬性已經改變。僅此而已。
所以:
public event PropertyChangedEventHandler PropertyChanged。
private bool m_isSaveButtonVisible = true;
public bool IsSaveButtonVisible
{
get => m_isSaveButtonVisible;
set
{
m_isSaveButtonVisible = value;
//如果有人監聽PropertyChanged,我們就告訴他IsSaveButtonVisible已經改變。
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsSaveButtonVisible)) )。
}
應該足夠了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/312720.html
標籤:
