我正在编写一个小项目来学习MVVM WPF。在项目中,我打开一个带有填充表单的按钮对话框窗口。表单有2个TextBoxes
,我想在每次再次打开对话框窗口时将它们清空。
在StackOverflow中有很多例子,其中TextBox绑定到用.Clear();
或string.Empty;
或.Text=""
清除TextBox的方法;等
问题是,我的TextBox已经与表单一起使用的转换器方法MultiBinded
。
<local:CustomContentDialogBox.CommandParameter>
<MultiBinding Converter="{StaticResource newPerson}">
<Binding ElementName="tbID" Path="Text"/>
<Binding ElementName="tbFirstName" Path="Text"/>
<Binding ElementName="tbSecondName" Path="Text"/>
<Binding ElementName="cbHeight" Path="Text" />
</MultiBinding>
</local:CustomContentDialogBox.CommandParameter>
就我的经历而言,我无法再次将它从texbox中绑定起来:
<TextBox Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2"
x:Name="tbFirstName"
FontSize="20"
Margin="10,0,0,0"
Background="#F5F5F5"
TextAlignment="Left"
Text="{Binding ClearMyBoxPlease}"
HorizontalAlignment="Left" Height="30" Width="150"/>
经过一整天的研究我发现,我可以在对话框窗口中添加按钮点击=&#34; Button_Click&#34;并在后面的代码中清理文本框。
<Button Grid.Row="0" Grid.Column="3"
VerticalAlignment="Top"
Margin="5,5,5,5"
Height="30" Width="80"
FontSize="20"
Background="#ccffe1"
Content="Add"
Click="Button_Click"
local:CustomContentDialogBox.CustomContentDialogResult="True">
</Button>
代码背后,我只想清理2个文本框:
private void Button_Click(object sender, RoutedEventArgs e)
{
tbFirstName.Text = "";
tbSecondName.Text = "";
}
供您参考,我的转换器类,从MultiBinding中获取TextBoxes:
public class PersonConverter : IMultiValueConverter
{
HeightListToString pzts = new HeightListToString();
public object Convert(object[] values, Type targetType, object parameter,
CultureInfo culture)
{
string id = (string)values[0];
string firstName = (string)values[1];
string secondName = (string)values[2];
Model.HeightList heightList = (Model.HeightList)pzts.ConvertBack(
values[3], typeof(Model.HeightList), null, CultureInfo.CurrentCulture);
return new ViewModel.PersonsList(id, firstName, secondName, heightList);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
CultureInfo culture)
{
throw new NotImplementedException();
}
}
此外,我试图在我的ViewModel类中使用ICommand
并将其与TextBox绑定。即使语法错误,程序也没有走过它的断点:
private ICommand clearMyBoxPlease;
public ICommand ClearMyBoxPlease
{
get
{
clearMyBoxPlease = new RelayCommand(
argument =>
{
TextBox tb = (TextBox)argument;
tb.Clear();
}
);
return clearMyBoxPlease;
}
}
我的观点是,只有一个代码背后给了我正确的效果。问题是,我的代码是针对MVVM模式理论的吗?如果是,我应该使用什么解决方案?提前谢谢。
更新
我刚试过:
<TextBox Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2"
x:Name="tbFirstName"
FontSize="20"
Margin="10,0,0,0"
Background="#F5F5F5"
TextAlignment="Left"
HorizontalAlignment="Left" Height="30" Width="150">
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotLost">
<i:InvokeCommandAction Command="{Binding ClearMyTextPlease}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
使用命令:
public ICommand ClearMyTextPlease
{
get
{
if (clearMyTextPlease == null)
clearMyTextPlease = new RelayCommand(
argument =>
{
TextBox tb = (TextBox)argument;
tb.Text = string.Empty;
}
);
return clearMyTextPlease;
}
}
它仍然没有通过ClearMyTextPlease
上的断点。
答案 0 :(得分:0)
好的,仅供将来参考。我在VM的对话框窗口中清除了TextBoxes,解决了我的问题。
我仍然无法将我的Text绑定到属性,可能是我做错了,但我使用了VisualTreeHelper并在关闭窗口之前调用了该方法。
方法:
static public void TraverseVisualTree(Visual myMainWindow)
{
int childrenCount = VisualTreeHelper.GetChildrenCount(myMainWindow);
for (int i = 0; i < childrenCount; i++)
{
var visualChild = (Visual)VisualTreeHelper.GetChild(myMainWindow, i);
if (visualChild is TextBox)
{
TextBox tb = (TextBox)visualChild;
tb.Clear();
}
TraverseVisualTree(visualChild);
}
}
在VM中调用dialg窗口类:
public CustomContentDialogBox()
{
execute =
o => //parametr "o" wyrażenia lambda
{
if (window == null)
{
window = new Window();
window.Width = WindowWidth;
window.Height = WindowHeight;
if (Caption != null) window.Title = Caption;
window.Content = WindowContent;
//(some logic)
TraverseVisualTree(window);
window = null;
}
};
}