Grief with WPF UserControl with ListBox and TextBo

2019-08-30 22:54发布

问题:

I am trying to produce a simple User Control that allows a user to enter notes and review the notes he or she has taken. For now, I have a ListBox to show previous notes and a TextBox to enter a new note. The ListBox also uses TextBoxs to show the individual notes. The logic is all working. Now I'd just like the control to look a little nicer. I am not seeing text wrapping when one of the notes is long. Nor am I seeing text wrapping on the TextBox where I enter a new note. I have tried to research this and have found the warnings about using StackPanels. Hence no stack panels here! Here is my XAML

<UserControl.Resources>
    <DataTemplate x:Key="DefaultTemplate">
        <Grid x:Name="GridItem" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition  />   
            </Grid.ColumnDefinitions>
            <TextBox x:Name="NoteText" Grid.Column="0"   Margin="0,5,5,0" Text="{Binding Path=NoteText}" TextWrapping="Wrap" />               
        </Grid>
    </DataTemplate>
</UserControl.Resources>
<Grid Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBox Grid.Row="0"  Text="{Binding Path=NewNoteText, UpdateSourceTrigger=PropertyChanged}"   LostFocus="TextBox_LostFocus">
        <TextBox.InputBindings>
            <KeyBinding Command="{Binding Path=AddNote}" Key="Enter"/>
        </TextBox.InputBindings>
    </TextBox>

        <ListBox    Grid.Row="1"  ItemsSource="{Binding Path=Notes}"  Margin="5" ItemTemplate="{DynamicResource DefaultTemplate}"   SelectionChanged="NotesControl_SelectionChanged">
        </ListBox>


</Grid>

I have tried specifying Width="*" in the UserResources. That made no difference. I don't really want to specify a maximum width for either TextBox if I can help it.

And here is how I use it in the main test form:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" ></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition ></RowDefinition>
    </Grid.RowDefinitions>

    <ctrlLib:RecordingListControl Grid.Row="0" Name="_ctrlRecordings" 
        PatientIdAndSessionNumber="{Binding  PatientIdAndSessionNumberMain, Mode=TwoWay}"                           
        SelectedItem="{Binding  SelectedItemMain, Mode=OneWayToSource}" />
    <Label Grid.Row="1" Content="{Binding SelectedItemMain.RecordingName}" />
    <ctrlLib:NotesControl Grid.Row="2"  Name="_ctrlNotes" PatientIdAndSessionNumber="{Binding PatientIdAndSessionNumberMain, Mode=TwoWay}" />

</Grid>

Any ideas? I get the general idea that nothing is telling the TextBox that it's width is constrained so no TextWrapping is invoked, but I'm at a loss as to how to tel the TextBox that there is a width constraint. I've seen all sorts of posts about not using "Auto" and using "*" but nothing seems to work!

Looking ahead, I'm not really happy with how this looks period. I'm currently getting a line drawn around every note item which I don't like. Also, I'd like the textbox where you enter the new note to look like part of the listbox and appear at the bottom. Thanks, Dave