【问题标题】:XAML controls binding with another controls which are in datatemplate !XAML 控件与数据模板中的另一个控件绑定!
【发布时间】:2011-03-17 04:57:58
【问题描述】:

我在 xaml 中有以下数据模板,在这里我在数据模板中有文本框,在普通 XAML 中有按钮,我真正想要的是我想要保持按钮被禁用,直到人们在文本框中输入内容?但它不起作用我已经尝试了以下代码

请调查一下并帮助我!或者有没有其他方法可以让我知道!

 <UserControl.Resources>
    <DataTemplate x:Key="dataTemplateParameter">            
        <StackPanel Width="170" Height="Auto" Margin="5">
            <TextBlock Width="160" TextAlignment="Left" HorizontalAlignment="Left" Height="22" Text="{Binding Path=ParameterName, Mode=TwoWay}"/>
            <TextBox x:Name="txtboxParameter"  Width="160" Height="22" HorizontalAlignment="Left" Text="{Binding Path=ParameterValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </StackPanel>            
    </DataTemplate>

<Button  Grid.Row="6" Name="SearchSourceParamBtn" Content="Search" VerticalAlignment="Bottom" Margin="12,5,92,0" Height="20" Visibility="{Binding SearchSourceBtnVisibility}" Command="{Binding SearchCommand}">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="IsEnabled" Value="True"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Text,x:ElementName=txtboxParameter}" Value="">
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

谢谢 纳尔斯卡蒂

【问题讨论】:

  • 是的,您是正确的,但问题是参数值不在视图模型中,它来自直接连接的模型!

标签: wpf xaml data-binding datatemplate


【解决方案1】:

您可以通过
1) 将按钮绑定到 RelayCommand。那么,
2) 验证命令的 CanExecute 委托中的“ParameterValue”。

编辑: 更好的方法是在 ViewModel 中有 ParameterValue,但由于某种原因你不会这样做。然后,在 VM 中创建一个静态 ChangeNotifyable 属性,使用该属性在 CanExecute 委托中进行验证。

【讨论】:

  • 是的,你是对的,但问题是参数值不在 tat viewmodel 中,它来自直接连接的模型!
  • 嗬。但是,永远不要将模型直接绑定到视图。我会建议。
  • 其实这不是我像图书馆一样单独保存的模型
【解决方案2】:

请验证 Command 对象的 CanExecute 委托中的“ParameterValue”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 2013-04-26
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    相关资源
    最近更新 更多