WPF - 从样式设置器 ControlTemplate 内部访问父控件
当使控件不可修改时,我们将它们显示为 TextBox
以保持一致的样式。问题是 ComboBox
可以包含任何类型的数据,因此绑定 ControlTemplate TextBox
的 Text
属性并不像使用 那样简单>所选项目
。
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}, Path=????, Converter={StaticResource ResourceKey=ComboToTextConverter}, UpdateSourceTrigger=PropertyChanged}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
我的想法是使用 Converter
并发送整个 ComboBox
,以便它可以由 Converter
代码处理。有办法做到这一点吗?
欢迎任何其他建议!
When making controls non-amendable we display them as a TextBox
to keep a consistent style. The problem is that a ComboBox
can have any type of data so binding the Text
property of the ControlTemplate TextBox
is not as simple as using SelectedItem
.
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}, Path=????, Converter={StaticResource ResourceKey=ComboToTextConverter}, UpdateSourceTrigger=PropertyChanged}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
The idea I have is to use a Converter
and send the whole ComboBox
so it can be handled by the Converter
code. Is there anyway to do this?
Any other suggestions are welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
SelectedValue
和SelectedValuePath
属性:现在,这是您的
ComboBox
,当您在
ComboBox
上将IsReadOnly
属性设置为 true 时,它会变成TextBox
将选定的值作为其文本。you need to use the
SelectedValue
andSelectedValuePath
properties:and heres your
ComboBox
now when you set the
IsReadOnly
property to true on theComboBox
, it turns into aTextBox
with the selected value as its text.