我如何可以绑定一个文本框到TextBlock的长度是多少? [英] How can i Bind the Length of a Textbox to a Textblock?

查看:447
本文介绍了我如何可以绑定一个文本框到TextBlock的长度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助绑定WPF的一些事情。
我有文本框,加入WTO要求在DataGrid中显示的属性的值此列。

I need an help to bind some things on wpf. I have this column of textboxes, whitch show the value of a property in the Datagrid.

<DataGridTemplateColumn Header="Value" Width="150">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>               
            <TextBox Width="150" Name="PropertyTextBox" Text="{Binding Path=Property.Value.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Path=Property, Converter={StaticResource isSimpleJPropertyConverter}, Mode=OneWay}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

我需要显示在另一列文本框中的实际长度,如果我改变文本框的文本长度也必须改变。

I need to show the actual Length of the textbox in another column, and if i change the text of the textbox the Length must change too.

<DataGridTemplateColumn Header="Leng" Width="150">
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Width="150" Text="{Binding Path=Text.Length, ElementName=PropertyTextBox, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

为什么这个不工作?任何提示吗?我该怎么办呢?

Why this don't work? Any tips? How can i do it?

编辑:全XAML

<Window x:Class="WpfInterceptor.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="this"
    xmlns:local="clr-namespace:WpfInterceptor"
    Title="MainWindow" Height="728" Width="755" Loaded="Window_Loaded">
<Window.Resources>

    <!--<local:JPropertyConverter x:Key="jPropertyConverter" />-->
    <local:IsSimpleJPropertyConverter x:Key="isSimpleJPropertyConverter" />
    <local:LengthConverter x:Key="lengthConverter" />

</Window.Resources>
<Grid DataContext="{Binding ElementName=this}">

    <Button x:Name="btn6" Content="Vai a Host" HorizontalAlignment="Left" Margin="662,172,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
    <TextBox x:Name="txt1" HorizontalAlignment="Left" Height="157" Margin="10,10,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="727"/>
    <RadioButton x:Name="rb2" Content="MID" HorizontalAlignment="Left" Margin="305,175,0,0" VerticalAlignment="Top" Checked="rb2_Checked"/>
    <RadioButton x:Name="rb1" Content="JSON" HorizontalAlignment="Left" Margin="246,175,0,0" VerticalAlignment="Top" Checked="rb1_Checked"/>
    <Button x:Name="btn5" Content="Clear" HorizontalAlignment="Left" Margin="582,172,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

   <StackPanel>
    <DataGrid x:Name="dg" HorizontalAlignment="Left" Margin="10,199,0,0" VerticalAlignment="Top" Height="46" Width="727" />

        <DataGrid x:Name="dg2" ItemsSource="{Binding Path=Properties}"  AutoGenerateColumns="False" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.IsDeferredScrollingEnabled="True" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" Height="350" IsEnabled="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=NomeCampo}" Header="Nome Campo" IsReadOnly="True" />
                <DataGridTextColumn Binding="{Binding Path=Lunghezza}" Header="Lunghezza" IsReadOnly="True"  Width="50" />

                <DataGridTemplateColumn Header="Valore" Width="150">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Canvas Background="Azure">
                               <!-- <TextBox Name="PropertyTextBox" Text="{Binding Path=Property.Value.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  /> seconda-->
                                <TextBox Width="150" Name="PropertyTextBox" Text="{Binding Path=Property.Value.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Path=Property, Converter={StaticResource isSimpleJPropertyConverter}, Mode=OneWay}" />
                                <!--<TextBox Width="250"  Text="{Binding Path=Property, Converter={StaticResource jPropertyConverter},  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged , Delay=500}" /> prima versione-->
                            </Canvas>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <!--<DataGridTextColumn Header="Lunghezza2" IsReadOnly="True"  Width="50" Binding="{Binding Path=Property.Value.Value.Length, UpdateSourceTrigger=PropertyChanged, Mode=OneWay, Converter={StaticResource lengthConverter}}"  />-->
                <DataGridTextColumn Header="Lunghezza2" IsReadOnly="True" Width="50" Binding="{Binding Path=Lunghezza2, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />


                <DataGridTemplateColumn Header="Leng" Width="150">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Width="150" Text="{Binding Path=PropertyTextBox.Text.Length, ElementName=PropertyTextBox, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

            </DataGrid.Columns>
        </DataGrid>

    </StackPanel>

    <Button x:Name="btn2" Content="Mostra Campi" HorizontalAlignment="Left" Margin="90,172,0,0" VerticalAlignment="Top" Width="91" Click="Button_Click_2"/>
    <Button x:Name="btn3" Content="Apri file" HorizontalAlignment="Left" Margin="10,172,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_3"/>
    <Button x:Name="btn4" Content="Accetta Modifiche" HorizontalAlignment="Left" Margin="406,172,0,0" VerticalAlignment="Top" Width="171" Click="Button_Click_4" IsEnabled="False"/>

</Grid>

推荐答案

的DataGrid列不在相同的视觉树。因此,你不能使用绑定的 的ElementName

DataGrid columns are not in same visual tree. Hence you can't bind using ElementName.

绑定与你的模型对象的属性,因为它已经双向绑定文本框与

Bind with property from your model object because it's already binded TwoWay with TextBox:

<DataTemplate>
    <TextBlock Width="150" Text="{Binding Path=Property.Value.Value.Length}" />
</DataTemplate>

这篇关于我如何可以绑定一个文本框到TextBlock的长度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆