locked
DataBinding inside a DataTemplate

    Question

  • I have a DataTemplate that contains several Buttons. I want to set the IsEnabled property of these Buttons based on whether certain TextBoxes have had values entered. I was planning on doing this using DataBinding and an IValueConverter that I have created. However, because the Button and TextBox are inside of a DataTemplate, I am having trouble figuring out what my databinding expression for the IsEnabled attribute should be. I tried the following:
    IsEnabled="{Binding Parent.Parent.Children[0].Text,RelativeSource={RelativeSource Mode=Self},Converter={StaticResource IsNullOrWhiteSpace},ConverterParameter=NOT}"
    However, this did not seem to work. What is the best way to do databinding inside of a DataTemplate? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Saturday, December 20, 2014 3:38 AM

Answers

  • Is thia you want ?

    <Page
        x:Class="App2.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App2"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    <Page.Resources>
            <local:ButtonEnabledConverter  x:Key="buttonenabledconverter"/>
            
    </Page.Resources>
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <ListView x:Name="listview" Margin="24">
                <ListView.ItemTemplate >
                      <DataTemplate>
                    <Grid  Background="Blue" Margin="6" >
                        <Grid.RowDefinitions >
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                        </Grid.RowDefinitions>
                        <TextBox x:Name="textbox" Width="80" Text="{Binding }"/>
                        <Button Grid.Row="1" Content="Click" IsEnabled="{Binding ElementName=textbox , Path=Text, Converter={StaticResource buttonenabledconverter} }"/>
                    </Grid>
                </DataTemplate>
                </ListView.ItemTemplate>
              
            </ListView>
        </Grid>
    </Page>
    

    namespace App2
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                List<string> s = new List<string>();
                for (int i=0;i <10;i++)
                {
                    s.Add(i.ToString());
                }
                listview.ItemsSource = s;
            }
        }
    }

    namespace App2
    {
        public class ButtonEnabledConverter : IValueConverter
        {
    
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                if (value!= null)
                {
                    string data = (string)value;
                    if (data !=string.Empty)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                throw new NotImplementedException();
            }
        }
    }


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Saturday, December 20, 2014 5:03 AM