我需要帮助来绑定一些关于wpf的东西。 我有这一列文本框,whitch显示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>
我需要在另一列中显示文本框的实际长度,如果我更改文本框的文本,则长度也必须更改。
<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>
为什么这不起作用?有小费吗?我该怎么办?
编辑:完整的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>
答案 0 :(得分:1)
DataGrid列不在同一个可视树中。因此,您无法使用 ElementName
进行绑定。
与模型对象中的属性绑定,因为它已经使用TextBox绑定了TwoWay:
<DataTemplate>
<TextBlock Width="150" Text="{Binding Path=Property.Value.Value.Length}" />
</DataTemplate>