我在我的MainWindow中有一個簡單的用戶控件。我正在嘗試創建一個易於移植的簡單窗口模板。我有一個用戶控件,這似乎服務於我的目的到目前爲止......還有...在usercontrol中設置WindowStyle和AllowTransparency
如果我在UserControl中設置了AllowTransparency和WindowStyle,項目編譯成功,但在控件的MainWindow中,我得到的對象未設置到...的實例並且整個控制被強調。如果我在MainWindow中設置,所有的都很好。我可以忍受這一點,但不是期望的結果。
XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cntl="clr-namespace:Rtg"
Title="MainWindow" Height="400" Width="525">
<!-- if I add windowstyle and allowtransparency here all good -->
<Grid>
<cntl:test1 WinTitle="test window framing">
<cntl:test1.PlaceHolder1>
<Grid Background="Orange">
<TextBox BorderBrush="Transparent" Text="Content in placehoder:" Height="35" Width="175" Margin="0,10,270,0" Background="Transparent" FontSize="16" ></TextBox>
<Button Width="100" Height="35" Content="click me" FontSize="16"></Button>
</Grid>
</cntl:test1.PlaceHolder1>
</cntl:test1>
</Grid>
</Window>
C#
public test1() {
Window win = Application.Current.MainWindow;
win.WindowStyle = System.Windows.WindowStyle.None;
win.AllowsTransparency = true;
InitializeComponent();
ctrTest2.Title = WinTitle;
}
去過網絡爲過去15年中WPF有些很新,我發展。
這是WPF的正常行爲嗎? 有沒有圍繞這樣我可以在usercotrol中設置窗口屬性?
之前或之後並不重要。我犯了同樣的錯誤。我相信只有當MainWindow爲null時纔會出現問題。但是構造函數已經被調用並且MainWindow正在初始化過程中。 – Mike
這真的很奇怪,我實際上創建了一個演示項目並嘗試過,它對我來說工作得很好。也許嘗試與視圖模型並將其綁定爲屬性。 –