I am using Avalon Edit control inside an itemscontrol. The scrollviewer is not scrolling when the cursor position changes. here is the code
UserControl1.xaml
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
mc:Ignorable="d"
x:Class="WpfApplication2.UserControl1"
x:Name="UserControl">
<Grid x:Name="LayoutRoot">
<avalonedit:TextEditor/>
</Grid>
</UserControl>
MainWindow.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
xmlns:local="clr-namespace:WpfApplication2"
x:Class="WpfApplication2.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<Grid x:Name="LayoutRoot">
<ScrollViewer CanContentScroll="True" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible">
<ItemsControl ItemsSource="{Binding}" x:Name="ItemsCtrlSteps">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:UserControl1/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</Window>
Mainwindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
ItemsCtrlSteps.DataContext = new List<string> { "a", "b" };
}
}
And i dont want to show scrollbars inside each texteditor..please help!!
Related
I have the following code to fit the grid exactly to the window.
As you can see in the screenshot, the grid is running out of the window on the bottom and right side. Do you guys have any idea why this is happening? and how to do it properly?
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Topmost="true"
Background="Aqua"
WindowStartupLocation="CenterScreen"
DataContext="{Binding Main, Source={StaticResource Locator}}"
Title="MainWindow" Height="500" Width="500">
<Grid
Width="{Binding ActualWidth, RelativeSource = {RelativeSource AncestorType = {x:Type Window}}}"
Height="{Binding ActualHeight, RelativeSource ={RelativeSource AncestorType = {x:Type Window}}}">
<Border Opacity=".9" BorderBrush="Blue" BorderThickness="2"/>
</Grid>
</Window>
If you bind the actual widths and heights of the window, the Grid will exceed the Window, as the content area is smaller than the window itself. Remove the bindings. The Grid will be sized automatically to occupy the available space if you do not explicitly set a Width and Height.
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Topmost="true"
Background="Aqua"
WindowStartupLocation="CenterScreen"
DataContext="{Binding Main, Source={StaticResource Locator}}"
Title="MainWindow" Height="500" Width="500">
<Grid>
<Border Opacity=".9" BorderBrush="Blue" BorderThickness="2"/>
</Grid>
</Window>
i am trying to bind a Textblock's Text property of MainWindow to a Listbox's Items.Count of another Usercontrol, and the textblock of Mainwindow failed to read the value of binding source.
but if both textblock and listbox belong to the same usercontrol or window, the binding is fine.
i know i can define a property of in the usercontrol and make it exposed to other framework elements(e.g. textblock) of Mainwindow, and then it can be bond.
i just cannot understand why the binding to the listbox of another usercontrol failed. any suggestion will be greatly appreciated.
here below is the sample i made for better explanation.
UserControl Xaml:
<UserControl x:Class="stackoverFlow.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:stackoverFlow"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<StackPanel>
<ListBox x:Name="lbDemo">
<ListBoxItem>
<TextBlock Text="element 01"/>
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="element 02"/>
</ListBoxItem>
<ListBoxItem>
<TextBlock Text="element 03"/>
</ListBoxItem>
</ListBox>
<!--binding to the control of the same usercontrol is fine-->
<TextBlock Text="{Binding ElementName=lbDemo,Path=Items.Count,StringFormat='there are {0} items in the listbox.'}"/>
</StackPanel>
</UserControl>
MainWindow Xaml:
<Window x:Class="stackoverFlow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:stackoverFlow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<local:UserControl1 x:Name="userControlDemo"/>
<Separator Margin="0 5 0 15"/>
<!--binding failed-->
<TextBlock Text="{Binding ElementName=userControlDemo,Path=lbDemo.Items.Count, StringFormat='there are {0} items in the listbox of Usercontrol1'}"/>
</StackPanel>
</Window>
In your User control, you should add a get accessor like this:
public int ListCount {
get {
return this.lbDemo.Items.Count;
}
}
And in your Main Window, you call it like this:
<TextBlock Text="{Binding ElementName=userControlDemo, Path=ListCount, StringFormat='there are {0} items in the listbox of Usercontrol1'}" />
So, I've gone and did what Edd asked and rewrote this. The problem is a textblock with a dynamic resource as its content, updates properly when the textblock is on the mainwindow. But when I try to load page1 into a frame on the mainwindow, it loads but does not update the dynamic resource. I'm a total noob so not sure what I'm even looking for to find what's causing the issue, let alone fix it. Any help appreciated!
This is the application.xaml:
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp2"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
StartupUri="MainWindow.xaml">
<Application.Resources>
<sys:String x:Key="TestString">This string is for testing.</sys:String>
</Application.Resources>
</Application>
This is the MainWindow.xaml:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Frame x:Name="MainFrame" NavigationUIVisibility="Hidden"/>
<TextBlock HorizontalAlignment="Left" Margin="125,299,0,0" TextWrapping="Wrap" Text="{DynamicResource TestString}" VerticalAlignment="Top" Height="110" Width="515" FontSize="48"/>
<Button Content="Call Page" HorizontalAlignment="Left" Margin="165,270,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Button Content="Change Text" HorizontalAlignment="Left" Margin="490,270,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>
</Window>
This is the Page1.xaml:
<Page x:Class="Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1">
<Grid>
<TextBlock HorizontalAlignment="Left" Margin="125,140,0,0" TextWrapping="Wrap" Text="{DynamicResource TestString}" VerticalAlignment="Top" Height="110" Width="515" FontSize="48"/>
</Grid>
</Page>
And this is the MainWindow.xaml.vb (Code-Behind):
Class MainWindow
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
MainFrame.Content = New Page1
End Sub
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
Resources("TestString") = "Test Completed."
End Sub
End Class
When I run the program and press both buttons, this is the result:
Image of the result
The string updates for the Textblock on the MainWindow, but not for the one on Page1 called into the frame.
I tried following code but it doesnt work. Any suggestions?
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Window1" x:Name="Window1" Title="Window1"
Height="300" Width="300"
Icon="{Binding ElementName=Application.Current.MainWindow, Path=Icon}" >
<Grid>
</Grid>
This should work:
<Window ...
Icon="{Binding Path=MainWindow.Icon, Source={x:Static Application.Current}}">
<Grid>
</Grid>
</Window>
XAML:
<WrapPanel>
<TextBox ScrollViewer.VerticalScrollBarVisibility="Auto" AcceptsReturn="True"/>
<TextBox ScrollViewer.VerticalScrollBarVisibility="Auto" AcceptsReturn="True"/>
</WrapPanel>
As you can see at the following picture, the second TextBox is cut after it wrapped, to second line.
Picture:
Put the WrapPanel in a ScrollViewer if you want to be able to scroll its content:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Window1" Height="300" Width="300">
<ScrollViewer>
<WrapPanel>
<TextBox Width="400"></TextBox>
<TextBox Height="500" ScrollViewer.VerticalScrollBarVisibility="Visible"></TextBox>
</WrapPanel>
</ScrollViewer>
</Window>
The WrapPanel won't automatically adopt to the size of the window.