ページ 1 / 1
グリッドの動的変更について
Posted: 2019年2月20日(水) 12:16
by Puutaro
XAML初心者です。
XAMLで以下のようにGridを定義しておき、何かの契機(例えばラジオボタン等)でcs側で変更することは
可能なのでしょうか?
それとも最初からGrid部分はcs側で新規作成しないといけないものでしょうか。
ご教授頂けますと幸いです。
XAML側
コード:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Name="InputGrid1" Height="235" />
<RowDefinition Name="InputGrid2" Height="120*" />
</Grid.RowDefinitions>
:
:
:
</Grid>
cs側
XAMLのInputGrid1、InputGrid2のHeightの値を変更
コード:
private void Radio_Checkd(object sender, RoutedEventArgs e)
{
//SetValueか何かを使用してInputGrid1とInputGrid2の
//Heightの値を変更する
}
以上、宜しくお願い致します。
Re: グリッドの動的変更について
Posted: 2019年2月20日(水) 17:38
by Math
ここ
rulesを読んで必要事項を記入お願い致します。
Windows10、VS2017Communication のWPFアプリケーションですか。
普通 可能ですよ。試せば簡単なはずですが。
Re: グリッドの動的変更について
Posted: 2019年2月20日(水) 18:18
by Math
[訂正]
>Windows10、VS2017Communication のWPFアプリケーションですか。
Windows10、VS2017Community のWPFアプリケーションですか。
Re: グリッドの動的変更について
Posted: 2019年2月20日(水) 19:06
by Puutaro
Math様
大変失礼いたしました。
開発環境は、Windows7(64bit)、VisualStudio2013を使用しての32bitアプリとして開発しております。
今回質問している部分を入れるまではWin10での動作確認もしております。
お聞きしたいところは、WPFのRowDefinitionで指定しているHeightの値をcs側で任意に変更できる
ものなのかというところになります。
①のコードを実行したところ、w_S_Height、w_M_Heightの値はそれぞれNaNでした。
また、②のコードを実行しても、RowDefinitionぶで指定しておりますInputGrid1、InputGrid2の
値に変化はありませんでした。
コード:
private void Radio_Checkd(object sender, RoutedEventArgs e)
{
//①
double w_S_Height, w_M_Height;
w_S_Height = Convert.ToDouble(this.InputGrid1.GetValue(Grid.HeightProperty));
w_M_Height = Convert.ToDouble(this.InputGrid2.GetValue(Grid.HeightProperty));
//②
w_S_Height = 0.0;
w_M_Height = 600.0;
this.InputGrid1.SetValue(Grid.HeightProperty, w_S_Height);
this.InputGrid2.SetValue(Grid.HeightProperty, w_M_Height);
}
お手数でございますが、宜しくお願い致します。
Re: グリッドの動的変更について
Posted: 2019年2月20日(水) 22:03
by Math
過去何度か答えたので過去ログでWPF を見たのですが出てこない。
C# WPF は Windows10、Windows7の機能だけで ザムル(XAML)のコンパイルも含めてビルド
出来ます。VisualStudio は不要です。メモ帳が使えればOKです。
(私は全角スペースを表示したくれC#の命令を色付き表示してくれるTerapad エディタを使用している)
App.xaml
App.xaml.cs
MainWindow.xaml
MainWindow.xaml.cs
を提示してください。
Re: グリッドの動的変更について
Posted: 2019年2月20日(水) 22:36
by Mathx
出来れば .xaml .cs と同じフォルダにある プロジェクト名.csproj をメモ帳で開いて
(テキスト・ファイルです)提示してください。
わからなければ MainWindow.xaml と MainWindow.xaml.cs だけでもいいですよ。
Re: グリッドの動的変更について
Posted: 2019年2月21日(木) 09:23
by Math
WPFがはじめてなら、まずここを読んで理解してください。
https://www.atmarkit.co.jp/ait/series/2794/
Windows Form とは全く違う DirectX をつかった システムなので レイアウトの仕方からして
違います。
コード全体を検証できる形で提示してくださいね。
Re: グリッドの動的変更について
Posted: 2019年2月21日(木) 10:23
by Puutaro
Math様、Mathx様
>コード全体を検証できる形で提示してくださいね
機密の関係から全体のアップはご勘弁ください。
現状プロジェクト全体のcsファイルだけで7万ステップをオーバーしております。
Win32API+DirectXの組み合わせは20年以上組んでおりますので、ある程度自信はあったのですが、
WPF+csは戸惑っております(スミマセン愚痴になりました)。
ピンポイントでグリッド内の<Grid.RowDefinitions>で定義しているHeightを契機によって
cs内で自由に変更できるかがわからなかったもので質問させて頂きました。
とりあえず自分でわかるXAML側でとりあえずGridだけ作成しておき、内容については
cs側の契機に合わせ、都度新規に作成する形にします。
色々とご教授ありがとうございました。
Re: グリッドの動的変更について
Posted: 2019年2月21日(木) 11:48
by Math
そうですか。了解です。
WPFはスマホのように画面の変化に対応してレイアウトを自動で変えるタイプなので迷いますよね。
(私もC# Formアプリケーションでエクセルなども制御して何万行というプログラムをかきましたよ)
(Mathxは 間違ってxがつきました)
Re: グリッドの動的変更について
Posted: 2019年2月21日(木) 12:00
by Math
[参考]
obj\Debug というフォルダにxaml が MainWindow.g.cs という形でC#に変換されており
それを見るとよく分かります。
Re: グリッドの動的変更について
Posted: 2019年2月21日(木) 12:34
by Math
[参考までにWindows10,Windows7 だけでBuildできるサンプルを載せておきます]
一つのフォルダーに次のファイルをメモ帳などでつくります。
App.xaml
コード:
<Application x:Class="test.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:test"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.cs
コード:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace test
{
/// <summary>
/// App.xaml の相互作用ロジック
/// </summary>
public partial class App : Application
{
}
}
MainWindow.xaml
コード:
<Window x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:test"
Title="MainWindow" Height="480" Width="640">
<Canvas>
<Rectangle
x:Name="rect"
Fill="Blue" Height="100" Stroke="Black" Width="100"
Canvas.Left="10" Canvas.Top="10"/>
<Button Content="right" Canvas.Left="432" Canvas.Top="10" Width="75" Height="40" Click="button1_Click"/>
<Button Content="down" Canvas.Left="432" Canvas.Top="55" Width="75" Height="40" Click="button2_Click"/>
<Button Content="set" Canvas.Left="432" Canvas.Top="100" Width="75" Height="40" Click="button3_Click"/>
</Canvas>
</Window>
MainWindow.xaml.cs
コード:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace test
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// 右へ移動
private void button1_Click(object sender, RoutedEventArgs e)
{
double x = Canvas.GetLeft(this.rect);
Canvas.SetLeft(this.rect, x + 20);
}
// 下へ移動
private void button2_Click(object sender, RoutedEventArgs e)
{
double y = Canvas.GetTop(this.rect);
Canvas.SetTop(this.rect, y + 20);
}
// 初期値へ戻す
private void button3_Click(object sender, RoutedEventArgs e)
{
Canvas.SetLeft(this.rect, 10);
Canvas.SetTop(this.rect, 10);
}
}
}
Re: グリッドの動的変更について
Posted: 2019年2月21日(木) 12:38
by Math
バッチファイル
b.bat
コード:
C:\windows\Microsoft.NET\Framework64\v4.0.30319\MSbuild.exe ^
csproj.txt
.\bin\Debug\test.exe
pause
csproj.txt
コード:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>WinExe</OutputType>
<RootNamespace>test</RootNamespace>
<AssemblyName>test</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Re: グリッドの動的変更について
Posted: 2019年2月21日(木) 12:46
by Math
Re: グリッドの動的変更について
Posted: 2019年2月21日(木) 13:08
by Math
Re: グリッドの動的変更について
Posted: 2019年2月21日(木) 18:30
by YuO
Puutaro さんが書きました: ↑6年前
お聞きしたいところは、WPFのRowDefinitionで指定しているHeightの値をcs側で任意に変更できる
ものなのかというところになります。
普通にHeightに代入すればよいです。
但し,RowDefinition.HeightはGridLength型であり,GridLength型はImmutableなので,
コード:
InputGrid2.Height = new GridLength(600.0, GridUnitType.Pixel);
のように,newして代入する必要があります。
Re: グリッドの動的変更について
Posted: 2019年2月21日(木) 18:47
by Puutaro
YuO様
ご回答、誠にありがとうございます。
まさにやりたいことでした。
解決しました、ありがとうございます。
Math様、いろいろご教授いただきましてありがとうございました。
以上でFIXとさせていただきます。
お二方、誠にありがとうございました。