对于我的生活,我似乎无法弄清楚设置ContentControl的背景颜色这个简单的任务:
<ContentControl x:Name="Content03"
Width="130"
Height="130"
Canvas.Top="50"
Canvas.Left="400"
Background="Yellow">
<Ellipse Fill="YellowGreen" IsHitTestVisible="True">
</Ellipse>
</ContentControl>
Run Code Online (Sandbox Code Playgroud)
也尝试使用样式这样做,但仍然无法工作;(
我正在构建一个MVVM WPF应用程序,其中有一个向导,在父视图(V)中加载了多个子视图,该视图使用ViewModel(VM)作为其数据上下文.其中一个MVVM模式原则指出VM不应该知道绑定它的V. 在这种情况下,VM确实不知道V,但是,VM(1)管理由不同子视图(即不同步骤)组成的列表属性,以及(2)具有另一个名为CurrentView动态分配元素的属性在列表中.在V中CurrentView受到约束ContentControl
我的问题是:
我是WPF的新手所以这可能是一个愚蠢的问题,但现在就是这样.
我正在尝试使用XAML创建自定义按钮,所以这就是我所拥有的:
<Button x:Class="Controls.ShinyButton"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Height="40" Width="150">
<Button.Resources>
<ResourceDictionary Source=".\Resources.xaml" />
</Button.Resources>
<Border Background="{StaticResource BlueGradient}" CornerRadius="5">
<DockPanel>
<Image x:Name="imgIcon" DockPanel.Dock="Left" Height="32" Margin="4"/>
<TextBlock DockPanel.Dock="Right" Text ="Test" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="White" />
</DockPanel>
</Border>
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF02B6FF" Offset="0" />
<GradientStop Color="#FF0084F0" Offset="1" />
</LinearGradientBrush>
</Button.Background>
</Button>
Run Code Online (Sandbox Code Playgroud)
设计人员理解它并且编译没有错误,但是当我尝试运行程序并创建它的实例时,我得到错误:
"ContentControl的内容必须是单个元素."
我的资源文件是:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<LinearGradientBrush x:Key="BlueGradient">
<GradientStop Offset="0" Color="#FF02B6FF"/>
<!-- (2, 182, 255)-->
<GradientStop Offset="1" Color="#FF0084F0"/>
<!-- (0, 132, 240) -->
</LinearGradientBrush>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
我的边框内有DockPanel,因此按钮只有一个子
节点...... …
我有一个自定义ContentControl,它具有固定的XAML布局,如UserControl(不是通常应用的通用模板).
以前这种布局没有额外的标记,所以它实际上是:
<ContentControl x:Class="MyControls.CustomViewControl"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
</ContentControl>
Run Code Online (Sandbox Code Playgroud)
这很好.
我现在想在内容周围添加边框,因此我将XAML更改为:
<ContentControl x:Class="MyControls.CustomViewControl"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<ContentControl.Template>
<ControlTemplate>
<Border BorderThickness="5" BorderBrush="LightGreen">
<ContentPresenter />
</Border>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
Run Code Online (Sandbox Code Playgroud)
这显示了边框,但没有内容.
我尝试为ContentPresenter提供显式绑定:
<ContentPresenter Content="{Binding Path=Content, RelativeSource={RelativeSource Self}}"/>
Run Code Online (Sandbox Code Playgroud)
但这没有任何区别.
设置显式Content确实有效:
<ContentPresenter Content="TEST" />
Run Code Online (Sandbox Code Playgroud)
任何人都知道为什么内容绑定不起作用?我想我可以回到通常的通用模板,但如果我可以像UserControl一样直接做它会更容易.
此问题与动态添加用户控件到caliburm micro有关.在打开这个新线程之前,我已经阅读过任何其他相关的线程,但我仍然不明白并找不到解决方案.如果你们中的一些人认为这是重复的,请接受我的道歉.
我有一个窗口(MainView)包含2列的"主"网格(又名LayoutRoot).
左栏有2个按钮:"显示视图1"和"显示视图2".
我的示例代码包含以下视图和视图模型:
在我的示例代码中,ContentControl无法识别UserControl.我究竟做错了什么?如何正确绑定ContentControl?请随意修改我的示例代码.先感谢您
MainView.xaml
<Window x:Class="TestCaliMiContentControl.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main View"
Width="525"
Height="350">
<Grid x:Name="LayoutRoot" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30*" />
<ColumnDefinition Width="100*" />
</Grid.ColumnDefinitions>
<StackPanel x:Name="LeftNavPanel" Grid.Column="0">
<Button x:Name="Display1" Content="Display View 1" />
<Button x:Name="Display2" Content="Display View 2" />
</StackPanel>
<ContentControl x:Name="MainGridContent" Grid.Column="1" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
MainViewModel.cs
public class MainViewModel : PropertyChangedBase
{
private ContentControl _mainGridContent;
public ContentControl MainGridContent
{
get { return _mainGridContent; }
set
{
_mainGridContent = …Run Code Online (Sandbox Code Playgroud) 鉴于以下两个选项:
<ContentControl>Content</ContentControl>
Run Code Online (Sandbox Code Playgroud)
与
<ContentControl Content="Content"/>
Run Code Online (Sandbox Code Playgroud)
我很害怕这个问题可能听起来有点幼稚,但我怎么知道我不会问,所以我决定拍它.
如果我选择了错误的决定,请评论我;)
我想知道如何在按下按钮时在主窗口中创建不同的视图.我不确定正确的术语,所以这阻碍了我的谷歌.
我认为主要的观看区域是内容控件,我可以在事件发生时进行更改.我做了一个小图画来帮助说明我的想法/想法.
任何输入将不胜感激.谢谢!
