单击清除按钮时如何清除 WPF 中的文本框
How to clear a textBox in WPF when a clear button is clicked
我有一个文本框,单击清除过滤器按钮时必须将其清除。但是它并没有通过分配 string.Empty 来清除。
XAML 文件:
<DataTemplate x:Key="FreeTextFilterTemplate">
<StackPanel Orientation="Vertical">
<Separator Style="{StaticResource SeparatorStyle}" Margin="5,10"/>
<Expander Header="{Binding FilterName}" FontWeight="Bold" IsExpanded="True" Margin="10,5" Style="{StaticResource ReversedExpanderStyle}">
<TextBox FontWeight="Normal" Margin="0,5,0,0" Text="{Binding FilterTextValue, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}" MaxLength="{Binding FilterMaxLength}"
helpers:TextBoxExtension.ValidationType="{Binding TextBoxValidateType}">
</TextBox>
</Expander>
</StackPanel>
</DataTemplate>
视图模型
private string _filtTextValue;
public string FilterTextValue
{
get => _filtTextValue;
set
{
if (_filtTextValue != value)
{
_filtTextValue = value;
RaisePropertyChangedEvent(FilterTextValue);
}
}
}
private void ClearSearchFilterModelData()
{
foreach (var filtType in SelectedCategory.Filters)
{
{
if (string.Equals(InventoryFilterNameEnum.Description.ToString(), filtType.FilterName.Replace(" ", string.Empty)))
{
if (!string.IsNullOrEmpty(filtType.FilterTextValue))
{
filtType.FilterTextValue = string.Empty;
}
}
除文本框外,其他控件正在被清除。
public void ButtonClickCommand(object parameter)
{
switch (parameter.ToString().ToLower())
{
case "clear_filter":
ClearFilter(); //optical-932
break;
case "close":
Close();
break;
case "search_filter":
GetInventoryFilterData();
break;
case "printlabels": //Optical-946
ShowPrintLabelsLegacy();
break;
}
}
private void ClearFilter() //optical-932
{
SelectedCategory = Categories.FirstOrDefault(x => x.CategoryId == InventoryConstants.CATEGORY_ID_FRAMES);
ClearFiltersOnSelectedCategory();
ItemCount = 0;
}
private void ClearFiltersOnSelectedCategory()
{
ClearSearchFilterModelData();
}
控件进入 if 条件,转到 属性,但未反映在 window。
确保您在 RaisePropertyChangedEvent 中传递 属性 的名称而不是内容。
public string FilterTextValue
{
get => _filtTextValue;
set
{
if (_filtTextValue != value)
{
_filtTextValue = value;
//Here >>>RaisePropertyChangedEvent(FilterTextValue);
}
}
}
我得到了解决方案。在双引号中指定 FilterTextValue。
public string FilterTextValue
{
get => _filtTextValue;
set
{
if (_filtTextValue != value)
{
_filtTextValue = value;
RaisePropertyChangedEvent("FilterTextValue");
}
}
}
我有一个文本框,单击清除过滤器按钮时必须将其清除。但是它并没有通过分配 string.Empty 来清除。
XAML 文件:
<DataTemplate x:Key="FreeTextFilterTemplate">
<StackPanel Orientation="Vertical">
<Separator Style="{StaticResource SeparatorStyle}" Margin="5,10"/>
<Expander Header="{Binding FilterName}" FontWeight="Bold" IsExpanded="True" Margin="10,5" Style="{StaticResource ReversedExpanderStyle}">
<TextBox FontWeight="Normal" Margin="0,5,0,0" Text="{Binding FilterTextValue, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}" MaxLength="{Binding FilterMaxLength}"
helpers:TextBoxExtension.ValidationType="{Binding TextBoxValidateType}">
</TextBox>
</Expander>
</StackPanel>
</DataTemplate>
视图模型
private string _filtTextValue;
public string FilterTextValue
{
get => _filtTextValue;
set
{
if (_filtTextValue != value)
{
_filtTextValue = value;
RaisePropertyChangedEvent(FilterTextValue);
}
}
}
private void ClearSearchFilterModelData()
{
foreach (var filtType in SelectedCategory.Filters)
{
{
if (string.Equals(InventoryFilterNameEnum.Description.ToString(), filtType.FilterName.Replace(" ", string.Empty)))
{
if (!string.IsNullOrEmpty(filtType.FilterTextValue))
{
filtType.FilterTextValue = string.Empty;
}
}
除文本框外,其他控件正在被清除。
public void ButtonClickCommand(object parameter)
{
switch (parameter.ToString().ToLower())
{
case "clear_filter":
ClearFilter(); //optical-932
break;
case "close":
Close();
break;
case "search_filter":
GetInventoryFilterData();
break;
case "printlabels": //Optical-946
ShowPrintLabelsLegacy();
break;
}
}
private void ClearFilter() //optical-932
{
SelectedCategory = Categories.FirstOrDefault(x => x.CategoryId == InventoryConstants.CATEGORY_ID_FRAMES);
ClearFiltersOnSelectedCategory();
ItemCount = 0;
}
private void ClearFiltersOnSelectedCategory()
{
ClearSearchFilterModelData();
}
控件进入 if 条件,转到 属性,但未反映在 window。
确保您在 RaisePropertyChangedEvent 中传递 属性 的名称而不是内容。
public string FilterTextValue
{
get => _filtTextValue;
set
{
if (_filtTextValue != value)
{
_filtTextValue = value;
//Here >>>RaisePropertyChangedEvent(FilterTextValue);
}
}
}
我得到了解决方案。在双引号中指定 FilterTextValue。
public string FilterTextValue
{
get => _filtTextValue;
set
{
if (_filtTextValue != value)
{
_filtTextValue = value;
RaisePropertyChangedEvent("FilterTextValue");
}
}
}