我在WPF应用程序的主窗口下面有以下XAML,我正在尝试设置d:DataContext下面的设计时间,我可以成功地为我的各种UserControls做,但是当我尝试在窗口...
Error 1 The property 'DataContext' must be in the default namespace or in the element namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'. Line 8 Position 9. C:\dev\bplus\PMT\src\UI\MainWindow.xaml 8 9 UI
<Window x:Class="BenchmarkPlus.PMT.UI.MainWindow"
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:UI="clr-namespace:BenchmarkPlus.PMT.UI"
xmlns:Controls="clr-namespace:BenchmarkPlus.PMT.UI.Controls"
d:DataContext="{d:DesignInstance Type=UI:MainViewModel, IsDesignTimeCreatable=True}"
Title="MainWindow" Height="1000" Width="1600" Background="#FF7A7C82">
<Grid>
<!-- Content Here -->
</grid>
</Window>
Run Code Online (Sandbox Code Playgroud) 对于XAML中的样式我需要这样的东西:
<Application.Resources>
#if DEBUG
<Style TargetType="{x:Type ToolTip}">
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FlowDirection" Value="LeftToRight"/>
</Style>
#else
<Style TargetType="{x:Type ToolTip}">
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FlowDirection" Value="RightToLeft"/>
</Style>
#endif
</Application.Resources>
Run Code Online (Sandbox Code Playgroud) 我的数据绑定版本号如下所示:
<Window <!-- ... --> DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<TextBlock>
Version is:
<Run Text="{Binding Version, Mode=OneWay}"></Run>
and advancing...
</TextBlock>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
它在运行期间正在运行.
如何在Visual Studio 2012的XAML编辑器中在设计时看到它?我只看到:
Version is: and advancing...
Run Code Online (Sandbox Code Playgroud)
代替:
Version is: 5.2.2 and advancing...
Run Code Online (Sandbox Code Playgroud)
下面的Jure的答案有效,但我最终使用了虚拟视图模型静态代码技术,这对我来说更好,因为数据是真实视图模型类型的模拟:
d:DataContext="{Binding Source={StaticResource DesignViewModel}}" ...
Run Code Online (Sandbox Code Playgroud) 这可能是一个愚蠢的问题,但是可以将一些示例数据定义为DataContext,以便在DesignView中查看我的DataTemplate吗?
目前,我总是要运行我的应用程序,看看我的更改是否正常.
例如,使用以下代码,DesignView只显示一个空列表框:
<ListBox x:Name="standardLayoutListBox" ItemsSource="{Binding myListboxItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" Content="{Binding text1}" />
<Label Grid.Column="1" Content="{Binding text2}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud) 我的绑定内容empty在UI设计模式中显示为字符串.我想为这些内容显示一些伪造的值,但我不知道如何.
如果你知道如何,请分享.谢谢!
我有一个基本的WinRT XAML UserControl,它有一个依赖属性,如下所示.用户控件显然只在另一个Page或UserControl中使用时才构造设计时.当我使用用户控件本身在设计器中工作时,不会呈现文本"Hello world".在这种情况下,如何让设计器使用数据初始化用户控件?
XAML:
<UserControl
x:Class="GTWin8.Ui.SimpleBinding"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:GTWin8.Ui"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
x:Name="ThisControl">
<Grid Background="Black">
<TextBlock Text="{Binding Path=Message, ElementName=ThisControl}"
HorizontalAlignment="Left" Margin="10,10,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Height="280" Width="380"/>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
代码隐藏:
public sealed partial class SimpleBinding : UserControl
{
public static DependencyProperty MessageProperty = DependencyProperty.Register(
"Message", typeof(String), typeof(SimpleBinding), new PropertyMetadata(null));
public String Message
{
get { return (String)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
public SimpleBinding()
{
this.InitializeComponent();
Message = "Hello world";
}
}
Run Code Online (Sandbox Code Playgroud) 我在XAML中定义了一个Label,如下所示:
<Label Content="{Binding Name}"></Label>
Run Code Online (Sandbox Code Playgroud)
问题是标签在XAML编辑器中是不可见的,我希望它在那里有一个默认值,以便编辑器准确地反映出在运行时显示的内容.
有没有办法在XAML中给它一个值,它将显示在编辑器中,但是然后在运行时使用绑定?
wpf ×6
xaml ×5
c# ×2
data-binding ×2
64-bit ×1
datatemplate ×1
design-time ×1
designmode ×1
designview ×1
windows-8 ×1
winrt-xaml ×1