Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF文本框所选文本不起作用_C#_Wpf_Textbox - Fatal编程技术网

C# WPF文本框所选文本不起作用

C# WPF文本框所选文本不起作用,c#,wpf,textbox,C#,Wpf,Textbox,我有一个带有静态资源的只读文本框控件,如下所示: <Style TargetType="TextBox" x:Key="MyEditTextEditor"> <Setter Property="FontFamily" Value="{Binding Path=TextEditorFontFamily, ElementName=LogViewerProperty}" /> <Setter Property="FontWeight" Value="{Bi

我有一个带有静态资源的只读文本框控件,如下所示:

<Style TargetType="TextBox" x:Key="MyEditTextEditor">
    <Setter Property="FontFamily" Value="{Binding Path=TextEditorFontFamily, ElementName=LogViewerProperty}" />
    <Setter Property="FontWeight" Value="{Binding Path=TextEditorFontWeight, ElementName=LogViewerProperty}" />
    <Setter Property="FontSize" Value="{Binding Path=TextEditorFontSize, ElementName=LogViewerProperty}" />
    <Setter Property="FontStyle" Value="{Binding Path=TextEditorFontStyle, ElementName=LogViewerProperty}" />
    <Setter Property="TextWrapping" Value="{Binding Path=WordWrapping, ElementName=LogViewerProperty, Converter={StaticResource BoolToTextWrap}}" />
    <Setter Property="Text" Value="{Binding Message, Mode=OneWay}" />
    <Setter Property="IsReadOnly" Value="True" />
    <Setter Property="Visibility" Value="Collapsed" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
              <Grid>
                <TextBox Text="{TemplateBinding Text}" BorderThickness="0" Margin="-3,-1"/>
              </Grid>
            </ControlTemplate>
          </Setter.Value>
         </Setter>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True">
            <Setter Property="Background">
              <Setter.Value>
                <SolidColorBrush Opacity="0.4" Color="{Binding Source={x:Reference LogViewerProperty}, Path=TextEditorSelectionColor}" />
              </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

是的,文本在控件中被选中,但属性为空。在ReadOnlyEdit.Text中存在正确的文本。

将数据绑定放入ListBoxItem,然后从ListBoxItem调用数据。然后整个ListBoxItem是可选择的。e、 g

<ListBox SelectionChanged="ListItemSelected" ItemTemplate="{StaticResource SelectedTextTemplate}">  
    <ListBox.ItemContainerStyle>

        <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
           .../...
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

.../...
在列表框之外构建数据模板或控制模板,例如

<DataTemplate x:Key="SelectedTextTemplate">
      <TextBox Text="{Binding SelectedText}"/>
</DataTemplate>

将数据绑定放入ListBoxItem,然后从ListBoxItem调用数据。然后整个ListBoxItem是可选择的。e、 g

<ListBox SelectionChanged="ListItemSelected" ItemTemplate="{StaticResource SelectedTextTemplate}">  
    <ListBox.ItemContainerStyle>

        <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
           .../...
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

.../...
在列表框之外构建数据模板或控制模板,例如

<DataTemplate x:Key="SelectedTextTemplate">
      <TextBox Text="{Binding SelectedText}"/>
</DataTemplate>


SelectedText是按字面选择的文本。如突出显示的文本中所示。如果未选择任何内容,则将返回空字符串。您确定不只是想要文本吗?您是否尝试过将模板中文本框的
SelectedText
绑定到
SelectedText
?正如在模板文本框上的
SelectedText=“{TemplateBinding SelectedText}”
中一样。不幸的是,SelectedText不是依赖属性-这种绑定是不可能的。我怀疑在文本框的控制模板中有一个文本框是一个聪明的主意。啊,这是一个很好的提示,谢谢!我注释掉part ControlTemplate并重试。现在它工作了!SelectedText是按字面选择的文本。如突出显示的文本中所示。如果未选择任何内容,则将返回空字符串。您确定不只是想要文本吗?您是否尝试过将模板中文本框的
SelectedText
绑定到
SelectedText
?正如在模板文本框上的
SelectedText=“{TemplateBinding SelectedText}”
中一样。不幸的是,SelectedText不是依赖属性-这种绑定是不可能的。我怀疑在文本框的控制模板中有一个文本框是一个聪明的主意。啊,这是一个很好的提示,谢谢!我注释掉part ControlTemplate并重试。现在它工作了!