开发者

XAML controls binding with another controls which are in datatemplate !

开发者 https://www.devze.com 2023-02-17 13:37 出处:网络
i have the following datatemplate in xaml and here i have textbox in datatemplate and button in normal XAML, what i really want is that i want to keep button be disabled until person enters something

i have the following datatemplate in xaml and here i have textbox in datatemplate and button in normal XAML, what i really want is that i want to keep button be disabled until person enters something in textbox ? but its not working i have tried the follwing code

Please look into it and help me as well ! or is there any other way to do this let me know !

 <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" />
                    <开发者_Python百科/DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

Thanks nallskarthi


You can achieve this by
1) binding button to RelayCommand. Then,
2) Validate the "ParameterValue" in command's CanExecute delegate.

Edit: The better way is to have the ParameterValue in ViewModel, but for some reason you won't do that. Then, create a static ChangeNotifyable property in VM, using this property do the validation in CanExecute delegate.

XAML controls binding with another controls which are in datatemplate !


Pls Validate the "ParameterValue" in the CanExecute delegate of Command object.

0

精彩评论

暂无评论...
验证码 换一张
取 消