我有一个我想与模型绑定的命令按钮列表(带输入)。问题是我希望按钮中的文本框绑定到某个地方(请参阅视图模型)。
以下代码是我尝试过但失败的代码。是否(甚至)可以在模型上设置绑定然后将其绑定到控件?
或者换句话说,我是否试图以愚蠢的方式做某事?
看法:
<ToolBar Grid.Row="1" Grid.ColumnSpan="2" Grid.Column="0" ItemsSource="{Binding SelectedTab.Commands}" Height="34">
<ToolBar.Resources>
<DataTemplate DataType="{x:Type model:ZoekCommandButtons}">
<Button Command="{Binding Command}" ToolTip="{Binding Tooltip}" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Image, Converter={StaticResource ImageConv}}" Height="16" Width="16"></Image>
**<TextBox Width="100" Text="{Binding Text}">**
<TextBox.InputBindings>
<KeyBinding Gesture="Enter" Command="{Binding Command}"></KeyBinding>
</TextBox.InputBindings>
</TextBox>
</StackPanel>
</Button>
</DataTemplate>
</ToolBar.Resources>
</ToolBar>
模型:
public class ZoekCommandButtons : BaseModel, ICommandItem
{
private string _header;
private string _image;
private bool _isEnabled;
private Visibility _isVisible;
private ICommand _command;
private string _tooltip;
private Binding _text;
public Binding Text
{
get { return _text; }
set { _text = value; OnPropertyChanged("Text"); }
}
(etc)
视图模型:
Commands.Add(new ZoekCommandButtons()
{
Image = "search.png",
IsEnabled = true,
**Text = new Binding { RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(UserControl), 1), Path = new PropertyPath("FilterText") },**
Command = FilterCommand,
Tooltip = "Zoeken",
Header = "Zoeken"
});