WPF 中的 EventHandler - 一个元素的更改会影响另一个元素
我有一个带有 TextBlock 和 ComboxBox 的堆栈面板,并且我有一个充当搜索字段的 TextBox。
<StackPanel Grid.Row="1" Grid.Column="2">
<TextBlock Text="Group Filter" Margin="7,0,0,0"/>
<ComboBox Text ="{Binding GroupFilter}" Height="30" Width="130" Margin="0,4,0,0"
Padding="5" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource GroupFilterEnum}}"
</ComboBox>
</StackPanel>
<TextBox Text="{Binding SingleFilter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.Column="3" Height="30" Width="140" Margin="0,20,0,0" Padding="5" materialDesign:HintAssist.Hint="Search single Id...">
</TextBox>
所以基本上我有一个数据集,您可以做的是您可以根据 GroupFilterEnum 中的预定义设置来过滤数据,或者您可以自己在文本框中输入过滤器值。
我尝试做的是:
当文本框为空时,事情应该保持原样。当 TextBox 具有值时,我想将以下内容添加到 ComboBox 以指示当 TextBox 中存在值时 ComboBox 处于非活动状态:
IsEditable="False"
IsHitTestVisible="False"
Focusable="False"
Foreground="{StaticResource MaterialDesignBodyLight}"
我尝试为 TextBox 创建一个 TextChanged EventHandler,例如:
private void textChangedEventHandler(object sender, TextChangedEventArgs args)
{
var text = sender.ToString().Replace("System.Windows.Controls.TextBox: ", "");
if (!string.IsNullOrWhiteSpace(text))
{
}
}
但我不这样做真正知道如何从该 EventHandler 引用 ComboBox。我对此还很陌生,所以我还没有真正做过很多事件处理程序。这是这样做的方法吗?如果是,有人可以帮助我使用事件处理程序吗? 或者也许有更好的方法?直接在 xaml 文件中可能吗?
非常感谢所有帮助,如果需要更多信息,请告诉我。
提前致谢。
I have a stackpanel with a TextBlock and a ComboxBox and the I have a TextBox acting as a search field.
<StackPanel Grid.Row="1" Grid.Column="2">
<TextBlock Text="Group Filter" Margin="7,0,0,0"/>
<ComboBox Text ="{Binding GroupFilter}" Height="30" Width="130" Margin="0,4,0,0"
Padding="5" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource GroupFilterEnum}}"
</ComboBox>
</StackPanel>
<TextBox Text="{Binding SingleFilter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.Column="3" Height="30" Width="140" Margin="0,20,0,0" Padding="5" materialDesign:HintAssist.Hint="Search single Id...">
</TextBox>
So basically I have a data set and what you can do is you can filter the data based on either a predefined setting from theGroupFilterEnum or you can enter a filter value yourself in the TextBox.
What I try to do is:
When the TextBox is empty things should just be as they are. When the TextBox has a value I would like to add the following to the ComboBox to indicate that the ComboBox is inactive when there is a value in the TextBox:
IsEditable="False"
IsHitTestVisible="False"
Focusable="False"
Foreground="{StaticResource MaterialDesignBodyLight}"
I have tried to make an TextChanged EventHandler for the TextBox like:
private void textChangedEventHandler(object sender, TextChangedEventArgs args)
{
var text = sender.ToString().Replace("System.Windows.Controls.TextBox: ", "");
if (!string.IsNullOrWhiteSpace(text))
{
}
}
But I don't really know how to refer to the ComboBox from this EventHandler. I am fairly new to this, so I haven't reallty done a lot of EventHandlers. Is this the way to do it? If yes, could someone please help me with the Eventhandler?
Or maybe there is a better way? Directly in the xaml file maybe?
All help is highly appreciated and please let me know if additional information is needed.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用需要实现的转换器将要更改的
ComboBox
属性绑定到TextBox
的Text
属性(这是示例)。You could bind the
ComboBox
properties you want to change to theText
property of theTextBox
using a converter that you would need to implement (here is the example).如果您想要一个可以像文本框或组合框一样使用的控件,那么您的方式并不好。
组合框控件有一个属性IsEditable,允许写入或选择一个值。
试试这个:
否则,您可以为控件指定一个名称,然后您可以通过键入他的名称在后面的代码中获取它。
结果如下:结果
(我对我的英语感到抱歉,我是法国人)。
If you want a control that can be used like a textBox or comboBox, you are not in the good way.
The comboBox control have a property IsEditable which permit to write or select a value.
Try this :
<ComboBox IsEditable="True"/>
Else, you can give a name to your control and you will able to get it in your code behind by typing his name.
The result here : Result
(And I'm sorry for my english, I'm french).